Insert Sort
public static int[] Sort (int[] a) {
int length = a.length;
for (int i = 1; i < length; i++) {
int key = A[i];
int j = i-1;
Why is j>=0 here? Because when j=0, if also conforms to the a[j]>key, also needs to carry on the shift operation. While
(J >= 0 && A[j] > key) {
A[j + 1] = A[j];
j--;
}
Why is j+1 here, because the last statement in the while loop, J--。
a[j + 1] = key;
}
return A;
}
Hill Sort
public class Shellsort {
static public int[] Sort (int[] a) {
int len = a.length;
int DK = LEN/2;
while (DK >= 1) {
//system.out.println (DK);
Shellinsert (A, DK);
DK/= 2;
}
return A;
}
/**
* DK = 1, simple insertion sort
/private static void Shellinsert (int[] A, int dk) {
if (DK = 0)
throw new IllegalArgumentException ("DK can ' t eqauls 0!");
int len = a.length;
for (int i = DK; i < Len; i++) {
int key = A[i];
int j = I-DK;
while (J >= 0 && key < A[j]) {
a[j + DK] = a[j];
J-=DK;
}
A[j + DK] = key;
}
}
The discussion of the DK in the order of Hill is more likely to refer to related books. binary Insertion Sort
public class Binaryinsertionsort {public
static int[] Sort (int[] a) {
int length = a.length;
for (int i = 1; i < length; i++) {
int key = A[i];
int low = 0;
int high = i-1;
High = Getinsertindex (A, a[i], low, high);
for (int j = i-1, J >= High; j--)
a[j + 1] = A[j];
A[high] = key;
}
return A;
}
private static int Getinsertindex (int[] A, int key, int low, int high) {while
(low <= high) {
int mid = + high)/2;
if (Key > A[mid]) {Low
= mid + 1;
} else {high
= mid-1;
}} return high + 1;
}
}