Select Sort * * * *
method Description : First find the first minimum number, and the first number exchange, then find the smallest and second exchange from the rest, and so on.
efficiency : An array of length n, approximately N2/2 comparison, N-Times Exchange
features : 1. Run-time and input-independent, ordered arrays, all equal arrays, the random array takes the same time, does not take full advantage of the initial state of the input.
2. The data movement is minimal, and the input is linearly related.
Code :
Sort (int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int min = i;
for (int j = i; J < N; j + +) {
if (A[j] < a[i])
Swap (a[i],a[j]);
}
}
}
Insert Sort * * * *
background : Playing poker
Method Description : The following number is inserted into the previously ordered sub-array
efficiency : The average movement of half the elements, so N2/4 comparison and exchange; Worst N2/2 comparisons and exchanges; best N-1 comparisons and 0 exchanges
features : For partially ordered arrays, the efficiency is very high and time is linear.
Several typical partial ordered arrays: Each element in the array is not far from its final position; an ordered large array is followed by a decimal group; only a few elements in the array are positioned incorrectly;
Code :
Sort (int[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
for (int j = i-1; J >= 0 && A[j] < a[i]; j--) {
Swap (a[i],a[j]);
}
}
}
Hill Sort * * * *
background : Large-scale insertion sorting is inefficient because it simply swaps adjacent elements. The idea of hill sort is to sort the subarray of h in a large array, and divide the array into approximately
The H-interval is a subarray of H, and gradually decreases h,h=1 when the insertion sort is actually inserted. The advantage of this is that at the beginning of the sorting, only the sub-arrays with the interval h are sorted, the order size is small and the speed is fast;
With the decrease of H, the array tends to be orderly, at which point the insertion order can play a full role.
Method Description : The following number is inserted into the previously ordered sub-array
efficiency : The average movement of half the elements, so N2/4 comparison and exchange; Worst N2/2 comparisons and exchanges; best N-1 comparisons and 0 exchanges
features : For partially ordered arrays, the efficiency is very high and time is linear.
Code :
Sort (int[] a) {
int n = a.length;
int h = 1;
while (H < N/3) H = 3*h+1;
while (H >= 1) {
for (int i = h; i < n; i++) {
for (int j = i; J >= 0 && A[j] < a[i]; J-= h) {
Swap (a[j],a[i]);
}
}
h = H/3;
}
}
Merge Sort * * * *
Method Description : Merging ideas
Efficiency : Nlogn
(1) in-situ merge code:
public static void merge (int[] A, int lo, int mid, int hi) {
int i = lo, j = mid+1;
int[] aux = new Int[a.length];
for (int k = lo; k <= hi; k++) {
AUX[K] = a[k];
}
for (int k = lo; k <= hi; k++) {
if (i > Mid) a[k] = aux[j++];//exception condition must be put in front, otherwise it may result in array out of bounds, report null pointer exception
else if (J > Hi) a[k] = aux[i++];
else if (A[i] < a[j]) a[k] = aux[i++];
else a[k] = aux[j++];
}
}
Sort by:
Sort (int[] A, int lo, int hi) {
if (Hi < lo) return;
int mid = lo + (Hi-lo)/2;
Sort (a,lo,mid);
Sort (A,mid+1,hi);
Merge (A,lo,mid,hi);
}
improvements : 1. Small size with insert sort
2. Test array is ordered, A[mid] < a[mid+1] think orderly, skip the merge method, this can make an orderly
The sub-array time is reduced to linear.
(2) Bottom-up merge sort:
methods : First 22 Merge, then 44 merge, then the,。。。。。
Code :
Sort (int[] a) {
int n = a.length;
Aux = new Int[n];
for (int i = 1; i < n; i + = I+i) {
for (int j = 0; j< n-i; j+= i+i) {
Merge (A,j,j+i-1,math.min (j+i+i-1,n-1));
}
}
}
features : Fit the data of the linked list organization, only need to reorganize the chain table link to be able to sort the list in place.
Quick Sort * * * *
features : in-situ sorting; Nlogn; The inner loop is small, the number of comparisons is less, and it is compared with fixed values, so it is very fast. For example, merge, insert sort, move elements inside loop, so slow
Compared to merge sort: Merge is recursive in front, handle array after; fast reverse
Code:
public static void sort (int[] A, int lo,int hi) {
if (Hi < lo) return;
Int J = partition (A,lo,hi);
Sort (a,lo,j-1);
Sort (A,j+1,hi);
}
static partition (int[] A, int lo, int hi) {
int i = lo, j = hi+1;
int guard = A[lo];
while (true) {
while (A[++i] < guard) if (i = = hi) break;
while (A[--j] > Guard) if (j = = lo) break;
if (i >= j) break;
Swap (a[i],a[j]);
}
Swap (a[lo],a[j]);
Return J;
}
Improved: A feature that is more efficient for fractional groups based on insertion sorting.
1. if (Hi <= lo) return, if (Hi <= lo+m) {insertion.sort (a,lo,hi); return;}
Summary of several common sorting algorithms