/**
* Simple insertion sort.
* 适合于基本有序的数组
* 稳定排序
* @param arr an array of Comparable item.
*/
public static <T extends Comparable<? super T>> void insertSort(T[] arr){
int j;
for (int p = 1; p<arr.length; p++){
T tmp = arr[p];
for (j = p; j>0 && tmp.compareTo(arr[j-1])==-1; j--){
arr[j] = arr[j-1];
}
arr[j] = tmp;
}
}
/**
* Shellsort,using Shell's(poor) increments.
* 希尔排序
* @param arr an array of Comparable item.
*/
public static <T extends Comparable<? super T>> void shellSort(T[] arr){
int j;
for (int gap = arr.length/2; gap > 0; gap/=2){
for (int i = gap; i < arr.length; i++){
T tmp = arr[i];
for (j = i; j >= gap && tmp.compareTo(arr[j-gap])==-1; j-=gap){
arr[j] = arr[j-gap];
}
arr[j] = tmp;
}
}
}
/**
* Internal method for heapsort.
* @param i the index of an item in the heap
* @return: the index of the left child.
*/
private static int leftChild(int i){
return 2*i + 1;
}
/**
* Internal method for heapsort that is use in deleteMax and buildHeap.
* @param arr an array of Comparable items.
* @int i the position form which to percolate down.
* @int n the logical size of the binary heap.
*/
public static <T extends Comparable<? super T>> void percDown(T[] arr, int i, int n){
int child;
T tmp;
for (tmp = arr[i]; leftChild(i) < n; i = child){
child = leftChild(i);
if(child != n - 1 && arr[child].compareTo(arr[child+1]) == -1){
child++;
}
else{
break;
}
}
arr[i] = tmp;
}
/**
* swapReferences arr[i] exchange with arr[j]
* @param arr an array.
* @int i
* @int j
*/
public static <T extends Comparable<? super T>> void swapReferences(T[] arr, int i, int j){
T tmp;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
/**
* Standard heapsort.
* @param arr an array of Comparable items.
*/
public static <T extends Comparable<? super T>> void heapSort(T[] arr){
/*buildHeap*/
for (int i = arr.length/2 - 1; i >= 0; i--){
percDown(arr, i, arr.length);
}
/*deleteMax*/
for (int i = arr.length - 1; i > 0; i--){
swapReferences(arr, 0, i);
percDown(arr, 0, i);
}
}
/**
* Internal method that makes recursive calls.
* @param arr an array of Comparable item.
* @param tmpArray an array to place the merged result.
* @param left the left-most index of the Subarray.
* @param right the right-most index of the Subarray
*/
private static <T extends Comparable<? super T>> void mergeSort(T[] arr, T[] tmpArray, int left, int right){
if( left < right){
int center = (left + right) / 2;
mergeSort(arr,tmpArray,left,center);
mergeSort(arr,tmpArray,center+1,right);
merge(arr, tmpArray, left, center + 1, right);
}
}
/**
* Internal method that merge tow sorted halves of a subarray.
* @param arr an array of Comparable item.
* @param tmpArray an array to place the merged result.
* @param leftPos the left-most index of the Subarray.
* @param rightPos the index of the start of the second half.
* @param rightEnd the right-most index of the Subarray
*/
private static <T extends Comparable<? super T>> void merge(T[] arr, T[] tmpArray, int leftPos, int rightPos, int rightEnd){
int leftEnd = rightPos - 1;
int tmpPos = leftPos;
int numElements = rightEnd - leftPos + 1;
//Main loop
while (leftPos <= leftEnd && rightPos <= rightEnd){
if(arr[leftPos].compareTo(arr[rightPos])==-1){
tmpArray[tmpPos++] = arr[leftPos++];
}
else {
tmpArray[tmpPos++] = arr[rightPos++];
}
}
//Copy rest of left half
while (leftPos <= leftEnd){
tmpArray[tmpPos++] = arr[leftPos++];
}
//Copy rest of right half
while (rightPos <= rightEnd){
tmpArray[tmpPos++] = arr[rightPos++];
}
//Copy tmpArray back
for (int i = 0; i < numElements; rightEnd--){
arr[rightEnd] = tmpArray[rightEnd];
}
}
/**
* Mergesort algorithm.
* @param arr an array of Comparable item.
*/
public static <T extends Comparable<? super T>> void mergeSort(T[] arr){
T[] tmpArray =(T[]) new Comparable[arr.length];
mergeSort(arr, tmpArray, 0, arr.length-1);
}