Hill sort
Also known as narrowing the incremental sort
Set an increment to the array first, the general array length is Len, and the increment is set to increment = len/2,increment each reduced to the original 1/2.
1. Group the numbers in the array by this increment: for example, 0,0+increment, 0+2*increment,... For a group of 1,1+increment, 1+2*increment,... For a different group.
2, for each group through the insertion algorithm to sort. (The binary insert sort has been written earlier, which is written in normal insert sort)
3, increment = INCREMENT/2, then continue the loop
Code: shell_sort.cc
#include <iostream> #include <vector>using namespace std;template< class T >void shell_sort (vector <T> &a) { int len = A.size (); int i,j; T tmp; int increment; for (increment = LEN/2; increment >0; Increment/= 2) {for (i = increment; i < len; i++) { tmp = a[i]; for (j = i; J >=increment; J-= increment) { if (tmp < a[j-increment]) a[j] = a[j-increment]; else{break ; } } A[J] = tmp;}} } int main (int argc, char **argv) { vector<int> a = {8, 9, 1, 7, 4, one,, 3, 2, 5}; Vector<int>::iterator iter; Shell_sort<int> (a); for (iter = A.begin (); ITER! = A.end (); iter++) { cout<< *iter << ""; } cout << Endl; return 0;}
Operation Result:
[Email protected]:~/program/datastruct/c_plus/sort$./a.out
1 2 3 4 5 7 8 9 10 11 23
Quick Sort
#include <iostream> #include <vector>using namespace Std;template<class t>int partition (vector<t > &a, int left, int. right) { int i = left; int j = right; T key = A[right]; for (j = left; J < right; J + +) { if (A[j] < key) { swap (a[j], a[i]); ++i; } } Swap (A[i], a[right]); return i;} Template<class t>void Quick_sort (vector<t> &a, int left, int.) { if (left < right) { int Q = partition (A, left, right); Quick_sort (A, left, q-1); Quick_sort (A, q+1, right);} } int main (int argc, char **argv) { vector<int> a = {8, 9, 1, 7, 4, one,, 3, 2, 5}; int len = A.size (); Vector<int>::iterator iter; Quick_sort<int> (A, 0, len-1); for (iter = A.begin (); ITER! = A.end (); iter++) { cout<< *iter << ""; } cout << Endl; return 0;}
Operation Result:
[Email protected]:~/program/datastruct/c_plus/sort$ g++-std=c++11 quik_sort.cc
[Email protected]:~/program/datastruct/c_plus/sort$./a.out
1 2 3 4 5 7 8 9 10 11 23
Heap Sort
Properties:
1. The binary heap is logically a completely binary tree, but we can store it in the form of arrays.
If the binary stack array is stored from 1, then the father I's left child subscript is 2i, the right child subscript is 2i+1; the father of the Empathy node I is labeled I/2.
If the binary stack array starts from 0, then the parent node I left child's subscript is 2i+1, and the right child's subscript 2i+2.
2, small Gan, the weight of a node is less than the weight of its other nodes in the subtree.
Dagen, the weight of a node is greater than the weight of the other nodes in the subtree.
Heap sequencing step;
1. Convert an ordinary array into a large heap array
2, A. Exchange the first number of Dagen and the last number of the array, and fix the large heap properties of the remaining number except the last number
B. Steps to repeat a
#include <iostream> #include <vector>using namespace Std;inline int left (int i) {return 2*i+1;} inline int Right (int i) {return 2*i+2;} Template<class t>void build_child_max_heap (vector<t> &a, int i, int len) {int max = i; if (left (i) < Len) {if (A[i] < A[left (i)]) max = left (i); if (right (i) < Len) {if (A[right (i)] > A[max]) max = right (i); } if (max! = i) {swap (A[max], a[i]); Build_child_max_heap (A, Max, Len); }}template<class t>void build_max_heap (vector<t> &a) {int len = a.size (); for (int i = (len-1)/2; I >= 0; i.) build_child_max_heap (A, I, Len);} Template<class t>void Heap_sort (vector<t> &a, int len) {build_max_heap (a); for (int i = len-1; i > 0; i.) {swap (a[0], a[i]); Build_child_max_heap (A, 0, i); }}int Main (int argc, char **argv) {vector<int> a = {6, 8, 9, 1, 7, 4, 11, 23, 3, 2, 5}; int len = A.size (); Vector<int>::iterator ITER; Heap_sort (A, Len); for (iter = A.begin (); ITER! = A.end (); iter++) {cout<< *iter << ""; } cout << Endl; return 0;}
Operation Result:
[Email protected]:~/program/datastruct/c_plus/sort$ g++-g-std=c++11 heap_sort.cc
[Email protected]:~/program/datastruct/c_plus/sort$./a.out
1 2 3 4 5 6 7 8 9 11 23
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
DIY data structure--Sorting algorithm 2 (Hill, Fast, heap) (C + + implementation)