当前位置:网站首页>Sort method (Hill, Quick, Heap)
Sort method (Hill, Quick, Heap)
2022-08-09 16:18:00 【Yellow duck 233】
These three are more important sorting methods, directly on the code
//Hill sortvoid shell_sort(int arr[], size_t n){int step;int i, j;for(step = n / 2; step > 0; step /= 2) // each time the number of steps is half of the current{for(i = step; i < n; i++){int key = arr[i]; //Be sure to record the value of the current i position firstfor(j = i - step; j >= 0 && key < arr[j]; j -= step) //j decreases step each timearr[j + step] = arr[j];if(j + step != i) //The value of the key will be assigned after there is an exchangearr[j + step] = key;}}}//quick sortvoid quick_sort(int arr[], size_t n){if(n <= 1)return;int left = 0, right = n - 1;int key = arr[0]; //take arr[0] as the identifierwhile(left < right){//First look from right to left, find the first value smaller than the key and put it in the left positionwhile(left < right && key <= arr[right])//With = sign, otherwise the same value will be exchanged and enter an infinite loopright--;arr[left] = arr[right];//Then look from left to right to find the first value larger than the key and place it on the rightwhile(lefty < right && arr[left] <= right)left++;arr[right] = arr[left];}//left == right after exiting the looparr[left] = key;//Repeat the above operation for the interval on the left and right of the key respectivelyif(1 < left)quick_sort(arr, left); //[0, left)if(left + 1 < n)quick_sort(arr + left + 1, n - 1 - left); //[left+1, n)}//build a large root heapvoid reheap(int arr[], size_t pos, size_t n){int key = arr[pos];int child = pos * 2 + 1; //Get the index of the left childwhile(child < n){if(child + 1 < n && arr[child] < arr[child + 1])child++; //The right child exists and is bigger than the left child;if(key < arr[child]){arr[pos] = arr[child];pos = child;child = pos * 2 + 1;}elsebreak;}arr[pos] = key;}// heap sortvoid heap_sort(int arr[], size_t n){int i;for(i = 2 / n - 1; i >= 0; i--) //the last branch node{reheap(arr, i, n);}for(i = n - 1; i >= 0; i--){int tmp = arr[0];arr[0] = arr[i];arr[i] = tmp;reheap(arr, 0, i); //put the maximum value to the end each time, re-adjust the big root heap to the previous number}}
边栏推荐
猜你喜欢
随机推荐
JVM简学笔记
redis6在centos7的安装
几何光学简介
Bessel function
排序方法(希尔、快速、堆)
原子的核型结构及氢原子的波尔理论
欢迎使用CSDN-markdown编辑器
Analysis: Which method is used to build a stock quantitative trading database?
通用的双向循环列表的几个比较重要的函数操作
[Mysql]--事务、事务的隔离级别、脏读、不可重复读、幻读解析
函数调用约定
For programming trading, focusing on forecast or on countermeasures?
什么是模板引擎?常见的模板引擎有哪些?thymeleaf的常用指令介绍。
流程控制学习
Redis6.2.1配置文件详解
多线程学习
How to use and execute quantitative programmatic trading?
bin document read and write
经典面试题 之 SQL优化
为什么要学编译原理