So-called sort. is to make a series of records, according to the size of one or some of the keyword. An ascending or descending array of operations.
Common sorting algorithms have selection sort, insert sort, hill sort, merge sort and high speed sort
Because the process of sequencing inevitably involves the comparison and exchange, so they are extracted into two separate functions, such as the following see
In order to sort the generality of the code, it is assumed that the element to be sorted implements the comparable interface private static Boolean less (comparable V, comparable W) {return V.compareto (W) <0;} private static void Exch (comparable[] A, int i,int j) {Comparable t = a[i];a[i] = A[j]; a[j] = t;}
Select Sort
Algorithm idea: first. Find the smallest element in the array. It then exchanges the position with the first element in the array.
Then find the smallest element from the rest of the elements. The second element in the array is swapped for the position, and so on, until the entire arrays are all ordered.
Algorithm features: 1. Execution time and input are irrelevant. Whether the input data is an ordered array, or a random array, it requires the same number of times.
2. The movement of the array is minimal.
Because each time you select a minimum element and then swap its position. So each exchange can schedule an element, so the total number of exchanges is N.
public static void Selectionsort (comparable[] a) {int n= a.length;for (int i=0;i<n;i++) {int min =i;//assumes the first number is minimum for (int J =i+1;j<n;j++) if (less (a[j],a[min])) min = j;//Gets the lowest element subscript Exch (A,i,min);//Swap location}}
Insert Sort
Algorithm idea: first. Assuming that the first element in the array is ordered, then inserting the second element into the appropriate position of the preceding ordered array, forming a new ordered array, and inserting the third element into the preceding ordered array, and so on, knowing that the array is generally ordered.
Algorithm features: The execution time is related to the initial input. Assuming that the initial input data has been ordered or partially ordered. The performance of the insert sort is better.
public static void Insertionsort (comparable[] a) {int N = a.length;for (int i=1;i<n;i++) {//element to be inserted subscript for (int j=i;j>0; j--)//Find the appropriate insertion position in the previously ordered array if (less (a[j],a[j-1])) Exch (A, J, j-1);}}
Hill sort
Algorithm idea: The idea of Hill sort is that the elements in the array that are arbitrarily spaced to H are ordered, and this array is called an H-ordered array. We keep lowering the value of h so that it finally becomes a 1-ordered array.
1-an ordered array, which is the overall order of the array, so the sorting is complete.
The hardest part of Hill sorting is the selection of the H sequence. There is no way to prove that a particular sequence is the best at the moment.
public static void sort (comparable[] a) {int N = A.length;int h = 1;//is used to record the step while (H<N/3) h=3*h+1;//step is 1,4,13,40......while ( H>=1) {for (int i=h;i<n;i++) for (int j=i;j>=h;j-=h)//a[i] INSERT into a[i-h],a[i-2*h],a[i-3*h] ... if (less (a[j],a[j-h))) Exch (a,j,j-h), h=h/3;//the steps previously recorded are reversed. Finally make it a 1-ordered array}}
Merge sort
Algorithm idea: To sort an array, recursively divide it into two halves to sort them, and finally merge the results together to make them overall orderly.
Merge sort is based on the idea of divide and conquer in algorithm design. We divide a big problem into small problems and solve them separately. Then use the answers to all the small questions to solve the big problem.
Algorithm features: 1. Array ordering with random length n is proportional to the time required to Nlogn.
The sort tree that can be sorted by merge to get
2. The extra space it needs is proportional to n. Because a secondary array of length n is required for the merge process.
private static comparable[] temp;//auxiliary array. The member variable defined as a class public static void MergeSort (comparable[] a) {temp = new Comparable[a.length];sort (a,0,a.length-1);} A[lo the array: Hi] sort private static void sort (comparable[] a,int lo, int hi) {if (Lo>=hi) return;int mid = lo+ (Hi-lo)/2;sort (A,lo,mid) ;//The left is sorted by recursion sort (a,mid+1,hi);//recursive to the right to sort merge (A,lo,mid,hi); Merge the left and right sides}//will be two ordered array A[lo. Mid] and A[mid+1..hi] are merged into an ordered array, using a secondary array temppublic static void merge (comparable[] a,int lo,int mid,int hi) {int i=lo,j=mid+ 1;for (int k=lo;k<=hi;k++)//copy array A to secondary array temp temp[k] = a[k];for (int k=lo;k<=hi;k++) if (i>mid) a[k]=temp[j++] ///left half, copy directly the remaining elements of the right half to the array else if (J>hi) a[k] = temp[i++];//the right half, directly copy the left half of the remaining elements into the array else if (less (temp[j],temp[i])) a[ K]=temp[j++]; The current element of the right half is smaller than the current element of the left half, and the current element of the right half of the elsea[k]=temp[i++];//left side is smaller than the current element of the right half, taking the left half}
High-speed sequencing
Algorithm idea: High-speed sequencing is a sort algorithm based on divide and conquer.
It divides an array into two word groups through a division. And the two word groups are sorted independently of each other.
public static void sort (comparable[] a) {sort (a,0,a.length-1);} private static void sort (comparable[] a,int lo, int hi) {if (Hi<=lo) return; int J = partition (A, lo, hi);//Shard Sort (a,lo,j -1)///left half sorted sort (a,j+1,hi);//Right half sort}private static int partition (comparable[] a,int lo,int hi) {int i=lo,j=hi+1;// Left and right scan pointer comparable v = a[lo];//slice element. After the element is finished, the position will be fixed and returned while (true) {//looping scan left and right pointers while (less (A[++I],V)) if (I==hi) break;while (Less (v,a[--j])) if (J==lo) break; if (i>=j) break;exch (a,i,j);} Exch (A,LO,J);//Put v=a[j] in the correct position return j;//returns the location of the Shard element}
In the file to be sorted, if there are multiple keyword same records, the relative order between these records with the same keyword is maintained, and the sorting method is stable. If the relative order between records with the same keyword changes, it is said that this sort method is unstable.
Sorting method |
Average Time |
Worst case scenario |
Degree of stability |
Extra Space |
Note |
Choose |
O (N2) |
O (N2) |
Not stable |
O (1) |
n hours better |
Insert |
O (N2) |
O (N2) |
Stability |
O (1) |
Most are sorted better |
Shell |
O (NLOGN) |
O (NS) 1<s<2 |
Not stable |
O (1) |
S is the selected grouping |
High speed |
O (NLOGN) |
O (N2) |
Not stable |
O (NLOGN) |
better when n is big |
Merge |
O (NLOGN) |
O (NLOGN) |
Stability |
O (1) |
better when n is big |
The process of this algorithm only focus on implementation, and no practical stack and other mechanisms to eliminate recursion,
Complete program: http://download.csdn.net/detail/danchu/7276229
Summary of common sorting algorithms (Java Implementation)