Choice: 1, outer circulation I, range N-1, the last one must be minimum.
2, for n size, need to compare n times.
3, the run time and the input order disorderly random is irrelevant, because must traverse
4, data movement is the least.
5, optimization is the heap sort
public static void Selectsort (comparable[] a) {
int N = A.length;
int mins;
for (int i = 0; i < N-1; i++) {
mins = i;
for (int j = i + 1; j < N; J + +) {
//
if (Sortutils.less (A[j], A[mins])) {
mins = j;
//}
Sortutils.exch (A, I, mins);
//}
//}
//}
Insert Sort: 1, generally is N2/4 compare and exchange, the worst is N2/2, preferably is N-1 compare and 0 times Exchange
2, platoon order will be much faster than random and reverse number
3, decimal groups are often inserted, will be faster than recursion, such as fast row and merge
public static void Insertsort (comparable [] a) {
int n = a.length;
//
for (int i = 1; i < n; i++) {
for (int j = i; J >0 && Sortutils.less (A[j], a[j-1]), j--) {
Sortutils.exch (A, J, j-1);
//}
//}
Hill: 1, insert optimization
2, through the H ordered data, avoids if the primary key is the smallest element just at the end of the array, then the insertion sort efficiency is not high situation
3, incremental sequence h= 3h+1, the highest efficiency in testing
4, through the while loop to achieve h, and then through the H>=1 loop to control the grouping, by the H=H/3 to reduce the group distance
public static void Shellsort (comparable[] a) {
//
int n = a.length;
int h1 = 1;
while (H1<N/3) {
H1 = 3*h1+1;
//
//
//}
while (h1>=1) {
for (int i = h1; i < n; i++) {
//
for (int j = i+1; J >=h1 && Sortutils.less (a[j],a[j-h1]); j=j-h1) {
Sortutils.exch (A, J, j-h);
//}
//}
H1=H1/3;
//
//}
//}
Merge:
1. Divide and conquer thought
2, the small-scale data n=15 around, using the insertion sort, test fast 10%-15%
3, test data is ordered, is a[mid]<a[mid+1] is orderly, out of the merge method
4. Use Arraycope to copy elements to auxiliary arrays. Underlying JNI. and auxiliary array aux does not declare a local variable in the merge because it reduces efficiency
5, 4 conditions to judge: the left side of the board exhausted (take the right half of the element), the right half exhausted (take the left side of the element), the right half of the current element is less than the current element of the left half (the right half of the element), the right half of the current element is greater than the current element on the left
6, in-situ merge cycle k, is the loop C "" A array, i=lo,j=mid+1, is aux array auxiliary a array
public static void Ms (comparable[] a) {
comparable[] Aux1 = A.clone ();
MS (A, aux1, 0, a.length-1);
}
private static void Ms (comparable[] A, comparable[] aux1, int lo, int hi) {
if (lo>=hi+10) {
Insertion.sort (a);
Return
}
int mid = lo + (Hi-lo)/2;
MS (A, Aux1, Lo, mid);
MS (A, AUX1, mid + 1, HI);
if (! Sortutils.less (Aux1[mid + 1], Aux1[mid])) {
System.arraycopy (Aux1, Lo, A, lo, Hi-lo + 1);
Return
}
M (A, Aux1, Lo,mid, HI);
}
private static void M (comparable[] A, comparable[] aux1, int lo, int mid,
int hi) {
Int J = Mid +1;
int i = lo;
for (int k = lo; k < hi; k++) {
if (I>mid) {
A[k]=aux1[j++];
}else if (J>hi) {
A[k]=aux1[i++];
}else if (sortutils.less (Aux1[j], aux1[i])) {
A[k]=aux1[j++];
}else {
A[k]=aux1[i++];
}
}
}
Quick line:
1, select the benchmark: in the sequence to be sorted, in a way to pick out an element, as a "benchmark"
2, split operation: The base in the sequence of the actual position, the sequence is divided into two sub-sequences. At this point, the elements on the left side of the datum are smaller than the datum, and the elements to the right of the datum are larger than the datum
3. Quickly sort two sequences recursively, until the sequence is empty or there is only one element.
4, three times to take the + interpolation + aggregation of equal elements + tail recursion + (multi-core multithreading) STL sort both efficiency is almost
private static int partition (comparable[] A, int lo, int hi) {
int i = lo;
Int j = Hi + 1;
Comparable v = A[lo];
while (true) {
while (Sortutils.less (A[++i], V)) {
if (i = = hi)
Break
}
while (Sortutils.less (V, A[--j])) {
if (j = = lo)
Break
}
if (i >= j) {
Break
}
Sortutils.exch (A, I, j);
}
Sortutils.exch (A, J, Lo);
Return J;
}
public static void QuickSort (comparable[] A, int lo, int hi) {
if (Hi <= lo + 10) {
Insertion.sort (a);
Return
}
int k = partition (A, lo, HI);
QuickSort (A, lo, k-1);
QuickSort (A, k + 1, HI);
}
Heap Sort:
1, 1 billion to take 10 maximum
2, from a "1" use, jump to a "0", in Exch and less, minus one
3. For loop sink constructs an ordered heap, while loop, sink is used to sort
4, sink in the While,2j<=h, first two sub-node comparison, the right side must be smaller than the left, like a retired old man, with the new Jin comparison, step by step, swim is floating
5, instead of using swim because of efficiency
6, the first sinking after floating. String or other part-time longer to sort
7. Unable to take advantage of cache
8. Is the only way we know that we can make the most of space and time. The worst is a 2NlgN comparison.
public static void HS (comparable[] a) {
int N = A.length;
for (int i = N/2; i > 0; i++) {
Sink1 (A, I, N);
//}
while (N > 1) {
Exch (A, 1, n--);
Sink (A, 1, N);
//}
//}
//
private static void Sink1 (comparable[] A, int i, int n) {
while (2*i<n) {
int h = 2*i;
if (H<n && less (A, H, h+1)) {
h++;
}if (!less (a,i,h)) {
Break
//}
Exch (A,i,h);
I=h;
//}
//}
Algorithm two, depth and optimization