Description : reproduced in http://www.cnblogs.com/qqzy168/archive/2013/08/03/3219201.html
Classification:
1) Insert sort (direct insert sort, hill Sort)
2) Swap sort (bubble sort, quick sort)
3) Select sort (Direct select sort, heap sort)
4) Merge sort
5) Allocation sort (cardinal sort)
Maximum required auxiliary space: merge sort
Minimum required secondary space: heap Ordering
Fastest Speed: Fast sorting
Unstable: Quick sort, hill sort, heap sort.
Let's take a look at the relationship between the 8 sorts:
1. Direct Insert Sort
(1) Basic idea: In a group of numbers to sort, assuming that the number of front (n-1) [n>=2] is already in the order, now you want to insert the nth number into the ordinal number in front, so that the n number is also in order. This cycle is repeated until all the rows are in order.
(2) Example
(3) Implementation in Java
Public static voidInsertsort () {inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; intTemp=0; for(inti=1;i<a.length;i++){ intJ=i-1; temp=A[i]; for(; j>=0&&temp<a[j];j--){ A[J+1]=A[J];//move the value greater than temp to one unit after the whole } a[j+1]=temp; } for(inti=0;i<a.length;i++){ System.out.println (A[i]); }}
2. Hill sort (minimum incremental sort)
(1) Basic idea: The algorithm will sort the group of numbers according to an increment D (n/2,n to the number of orders to be sorted) into groups, each group of records of the subscript difference D. Direct insert sorting of all elements in each group, followed by a smaller increment (D/2) to group it, The direct insert sort is then done in each group. When the increment is reduced to 1 o'clock, the sort is completed after the direct insert sort.
(2) Example:
(3) Implementation in Java
Public Static voidShellsort () {inta[]={1,54,6,3,78,34,12,45,56,100}; Doubled1=a.length; intTemp=0; while(true) {D1= Math.ceil (d1/2); intD= (int) D1; for(intx=0;x<d;x++){ for(inti=x+d;i<a.length;i+=d) { intj=i-D; Temp=A[i]; for(; j>=0&&temp<a[j];j-=d) {A[j+d]=A[j]; } a[j+d]=temp; } } if(d==1){ Break; } for(inti=0;i<a.length;i++) {System.out.println (a[i]); } } }
3. Simple selection of sorting
(1) Basic idea: In the group of numbers to be sorted, select the smallest number to exchange the number of the first position, and then in the remaining number to find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison.
(2) Example:
(3) Implementation in Java
Public Static voidSelectsort () {inta[]={1,54,6,3,78,34,12,45}; intPosition=0; for(inti=0;i<a.length;i++){ intJ=i+1; Position=i; inttemp=A[i]; for(; j<a.length;j++){ if(a[j]<temp) {Temp=A[j]; Position=J; }} A[position]=A[i]; A[i]=temp; } for(inti=0;i<a.length;i++) System.out.println (A[i]); } }
4, Heap sort
(1) Basic idea: heap sorting is a sort of tree selection, which is an effective improvement to the direct selection sort. The heap is defined as follows: A sequence with n elements (h1,h2,..., HN) is called a heap when and only if it satisfies (hi>=h2i,hi>=2i+1) or (hi<=h2i,hi<=2i+1) (i=1,2,..., N/2). Only the heap that satisfies the former condition is discussed here. As can be seen from the definition of a heap, the top element of the heap (that is, the first element) must be the largest (large top heap). A fully binary tree can represent the structure of a heap visually. Heap top is the root, the other is Zuozi, right subtree. The sequence of the numbers to be sorted is initially treated as a two-fork tree that is stored sequentially, adjusting their storage order to become a heap, when the heap has the largest number of root nodes. The root node is then exchanged with the last node of the heap. The number of fronts (n-1) is then re-adjusted to make it a heap. And so on, until there are only two nodes of the heap, and exchange them, and finally get an ordered sequence of n nodes. In terms of algorithm description, heap sequencing requires two processes, one is to build the heap, and the other is the last element of the heap to exchange the position. So the heap sort has two functions. One is to build the seepage function of the heap, and the second is to call the function of the infiltration function to realize the sorting.
(2) Example:
Initial sequence: 46,79,56,38,40,84
Build heap:
Swap, kicking the maximum number out of the heap
The remaining nodes are built again, and then the maximum number is kicked out.
And so on: The last two remaining nodes in the heap are exchanged, kicked out, and sorted.
(3) Implementation in Java
Public Static voidHeapsort () {inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; System.out.println ("Start sorting"); intArraylength=a.length; //Loop Build Heap for(inti=0;i<arraylength-1;i++){ //Build a heapBuildmaxheap (a,arraylength-1-i); //swap heap top and last elementSwap (a,0,arraylength-1-i); System.out.println (Arrays.tostring (a)); } } Private voidSwapint[] Data,intIintj) {inttmp=Data[i]; Data[i]=Data[j]; DATA[J]=tmp; } //build a large top heap from 0 to lastindex for the data arrayprivate void Buildmaxheap (int[] Data,intLastIndex) { //starting at the parent node of the lastindex node (the last node) for(intI= (lastIndex-1)/2;i>=0;i--){ //K Save the node being judged intk=i; //If the child node of the current K-node exists while(k*2+1<=LastIndex) { //index of the left child node of the K-node intBiggerindex=2*k+1; //if Biggerindex is less than lastindex, that is, the right child node of the K node represented by biggerindex+1 exists if(biggerindex<LastIndex) { //if the value of the right child node is large if(data[biggerindex]<data[biggerindex+1]){ //Biggerindex always records the index of a larger child nodebiggerindex++; } } //if the value of the K-node is less than the value of its larger child nodes if(data[k]<Data[biggerindex]) { //Exchange themswap (Data,k,biggerindex); //Assign the Biggerindex to K, start the next loop of the while loop, and re-guarantee that the value of the K-node is greater than the value of its left and right child nodesk=Biggerindex; }Else{ Break; } } } }
5. Bubble sort
(1) Basic idea: In the group of numbers to be sorted, the total number in the range that is not yet well sequenced, top-down
The adjacent two numbers are compared and adjusted sequentially, allowing the larger number to sink and the smaller one to go up. That is, whenever two adjacent
The comparison of the numbers shows that they are reversed when they are ordered in the opposite order.
(2) Example:
(3) Implementation in Java
Public Static voidBubblesort () {int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; intTemp=0; for(inti=0;i<a.length-1;i++){ for(intj=0;j<a.length-1-i;j++){ if(a[j]>a[j+1]) {temp=A[j]; A[J]=a[j+1]; A[j+1]=temp; } } } for(inti=0;i<a.length;i++) {System.out.println (a[i]); } }
7. Merge sort
(1) Basic sorting: Merge (merge) sorting method is to combine two (or more than two) ordered tables into a new ordered table, that is, to sort the sequence into a number of sub-sequences, each sub-sequence is ordered. Then the ordered subsequence is combined into a whole ordered sequence.
(2) Example:
8. Base Order
(1) The basic idea: to unify all the values to be compared (positive integers) to the same digit length, the number of the short number of the first complement 0. Then, start with the lowest bit and order one at a time. This is done from the lowest bit to the highest order.
Later, the series becomes an ordered sequence.
(2) Example:
The eight most commonly used sorting algorithms, including Java instance (copy)