Summary of internal sorting algorithms:
Sorting algorithms have their own time complexity, which does not mean that algorithms with high time complexity are less efficient in any case than time-complexity algorithms, and vice versa. The sorting algorithm can be divided into the following types according to its most basic operations:
- Insert Class Sort algorithm
- Direct insertion Sorting algorithm
- binary insertion Sorting algorithm
- Hill Sort algorithm
- Choosing a class sorting algorithm
- Simple selection sorting algorithm
- Heap Sort
- Exchange class sorting algorithm
1, direct insertion sorting algorithmalgorithm idea:
Inserts an unordered element into an ordered table that is already sorted sequentially
Code implementation:
1 /**2 Simple Insert Sort3 */4 voidSort::insertsort (int* Array,intsize) {5 inti,j,x;6 for(i=1; i<size;i++){7 if(array[i]<array[i-1]){//The first i-1 elements ordered, if array[i]>= array[i-1], then ordered, directly after the move,8j = I1;//if array[i]<array[i-1], insert forward to the appropriate position9x =Array[i];Ten while(x<Array[j]) { Onearray[j+1] =Array[j]; Aj--; - } -array[j+1] =x; the } - } -}
2, binary insertion sorting algorithmalgorithm idea:
Inserts an unordered element into an ordered table that is already sorted, because it is inserted into an ordered table, and you can find the location to insert first by binary.
Code implementation:
1 /*2 returns position I, so that x is inserted into the I and then becomes orderly3 */4 intBinarySearch (int*array,intSizeintx) {5 if(Size = =0)return 0;6 if(x<array[0])return 0;7 intHalf = size/2;8 if(X <Array[half])9 returnBinarySearch (array,half-1, x);Ten Else{ One returnHalf+binarysearch (array+half+1, size-half-1, X) +1; A } - } - /** the binary Insertion - */ - voidSort::binaryinsertsort (int* Array,intsize) { - inti,j,x,p; + for(i=1; i<size-1; i++){ - if(array[i]<array[i-1]){//The first i-1 elements ordered, if array[i]>= array[i-1], then ordered, directly after the move, +x = Array[i];//if array[i]<array[i-1], insert forward to the appropriate position Aj =BinarySearch (array,size,x); atp =i; - while(p>j) { -ARRAY[P] = array[p-1]; - } -ARRAY[J] =x; in } - } to}
3, Hill sortalgorithm idea:
Inserts an unordered element into an ordered table that is already sorted, because it is inserted into an ordered table, and you can find the location to insert first by binary.
Code implementation:
1 voidShellgroupsort (int*array,intSizeintStep) {2 inti,j,x;3 for(i=step;i<size;i++){4 if(Array[i] < array[i-Step]) {5x=Array[i];6j = IStep;7 while(x<Array[j]) {8Array[j+step] =Array[j];9j-=Step;Ten } OneArray[j+step] =x; A } - } - } the voidSort::shellsort (int*array,intsize) { - inti = size/2; - while(i>0){ - Shellgroupsort (array,size,i); +i = i/2; - } +}
4, simple select sort
algorithm idea:
In the set of numbers to be sorted, select the minimum (or maximum) number to exchange with the number of the 1th position, and then in the remaining number, find the minimum (or maximum) number of the 2nd position to exchange, and so on, until the first N-1 element (the penultimate number) and the nth element (the last number) to compare
Code implementation:
voidSort::selectsort (int*array,intsize) { intI=0, J; for(; i<size-1; i++){ intMinindex =i; for(j=i;j<size;j++) {//looking for the lowest element subscript if(Array[j]<array[minindex]) Minindex =J; } inttemp =Array[minindex]; Array[minindex]=Array[i]; Array[i]=temp; }}
5, Heap sort
algorithm idea:
The heap can be regarded as a complete binary tree, if the value of the two-tree non-leaf node is not greater than the value of its descendants, we call it a small heap, if the value of the leaf node is not less than its descendants of the node value, called a large heap, and the heap sort is to select the top element of the heap, that is, Replace it with the heap tail element (the top element of the heap will not be used as a heap element, i.e. not participate in the subsequent heap adjustment), and adjust the heap to be destroyed until only one element is left in the heap.
so , in addition to the first build of the heap, the process of heap sequencing is to constantly adjust the heap process, then how to adjust a destroyed heap it?
Adjustment heap
When the first element is replaced with the end-of-heap element, except that the heap-top element has no heap nature, the other heap elements are kept in a heap, so as long as the top element of the heap is adjusted to the corresponding position along its descendant nodes, the operation is as follows (in the case of a small heap):
Select the top element of the heap, displace it with a smaller descendant node, and recursively the process until it satisfies the small heap properties
Build a heap
It is not difficult to see that the heap is another property of the heap of arbitrary subtree is also a heap, so the process of building the heap is to adjust all its sub-tree to the process of the heap, we can start with the non-leaf node as the root of the subtree (because the leaf node root node subtree is already a complete heap), from back to front
Code implementation:
1 /*2 adjusting the heap, only the heap top element does not satisfy the nature of the heap3 */4 voidAdjustheap (int*array,intSizeintroot) {5 if(Root >= size/2)return;6 intLeftindex = (root+1)*2-1;7 intMinindex =Leftindex;8 intRightindex = (root+1)*2;9 //If there is a right child, and the right child is smaller than the left childTen if(Rightindex < size && Array[minindex] > array[rightindex]) minindex =Rightindex; One A if(Array[root] <=Array[minindex]) - return; - Else{ the inttemp =Array[root]; -Array[root] =Array[minindex]; -Array[minindex] =temp; - Adjuststack (array,size,minindex); + } - } + /* A the process of building a heap at */ - voidBuildheap (int*array,intsize) { - introot = size/2-1; - while(root>0){ - adjustheap (array,size,root); -root--; in } - } to voidSort::heapsort (int*array,intsize) { + buildheap (array,size); - while(size>1){ the inttemp = array[size-1]; *array[size-1] = array[0]; $array[0] =temp;Panax Notoginseng -Adjustheap (Array,size,0); thesize--; + } A}
6, bubble sort
algorithm idea:
In the set of numbers to be sorted, the total number in the range that is not currently in sequence, the top-down pairs of adjacent two numbers are compared and adjusted sequentially, so that the larger number to sink , smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.
Code implementation:
Summary of sorting algorithms