插入排序:
1.直接插入:
public static <T extends Comparable<? super T>> void insertionSort(T[] a)
{
int j;
for(int p = 1; p < a.length; p++)
{
T temp = a[p];
for(j = p; j > 0 && tmp.compareTo(a[j – 1]) < 0; j--)
a[j] = a[j – 1];//后移避免交换
a[j] = temp;
}
}
2.二分插入
public static <T extends Comparable <? super T>> void binaryInsertionSort(T[] a)
{
for(int p = 1; p < a.length; p++)
{
T temp = a[p];
int left = 0;
int right = p - 1;
int middle;
while(left <= right)
{
middle = (left + right) / 2;
if (temp.compareTo(a[middle]) < 0)
{
right = middle - 1;
}
else {
left = middle + 1;
}
}
for(int j = p; j >= left; j--)
{
a[j] = a[j - 1];
}
a[left] = temp;
}
}
3.希尔排序
public static <T extends Comparable<?super T>> void shellsort(T[] a)
{
int j;
for(int gap = a.length/2; gap > 0; gap /= 2)
{
for(int i = gap; i < a.length; i++)
{
T temp = a[i];
for(j = i; j >= gap && temp.compareTo(a[j - gap ]) < 0; j -= gap)
{
a[j] = a[j - gap];
}
a[j] = temp;
}
}
}
归并排序:
public static <T extends Comparable<? super T>> void mergesort(T[] a)
{
@SuppressWarnings("unchecked")
T[] temp = (T[]) new Comparable[a.length];
mergesort(a, temp, 0, a.length - 1);
}
/**
* Internal method that makes recursive calls.
*/
private static <T extends Comparable<? super T>> void mergesort(T[] a,
T[] tempArray, int left, int right)
{
if (left < right)
{
int center = (left + right) / 2;
mergesort(a, tempArray, left, center); // divide
mergesort(a, tempArray, center + 1, right);
merge(a, tempArray, left, center + 1, right); // conquer
}
}
/**
* Internal method that merges two sorted halves of a subarray.
*/
private static <T extends Comparable<? super T>> void merge(T[] a,
T[] tempArray, int leftPos, int rightPos, int rightEnd)
{
int leftEnd = rightPos - 1;
int numElements = rightEnd - leftPos + 1;
int tempPos = leftPos; // Cctr counter
while (leftPos <= leftEnd && rightPos <= rightEnd)
{
if (a[leftPos].compareTo(a[rightPos]) <= 0)
{
tempArray[tempPos++] = a[leftPos++];
}
else
{
tempArray[tempPos++] = a[rightPos++];
}
}
while (leftPos <= leftEnd) //Copy rest of first half
{
tempArray[tempPos++] = a[leftPos++];
}
while(rightPos <= rightEnd) //Copy rest of right halfw
{
tempArray[tempPos++] = a[rightPos++];
}
// copy tempArray back
for(int i = 0; i < numElements; i++, rightEnd--)
{
a[rightEnd] = tempArray[rightEnd];
}
}
选择排序:
1.简单选择
public static <T extends Comparable<? super T>> void selectionSort(T[] a)
{
for(int i = 0; i < a.length; i++)
{
T minItem = a[i];
int minIndex = i;
for(int j = i + 1; j < a.length; j++)
{
if (a[j].compareTo(minItem) < 0)
{
minItem = a[j];
minIndex = j;
}
}
a[minIndex] = a[i];
a[i] = minItem;
}
}
2.堆排序
public static <T extends Comparable<? super T>> void heapsort(T[] a)
{
for(int i = a.length/2; i >= 0; i--) //buildHeap
percolateDown(a, i, a.length);
for(int i = a.length - 1; i > 0; i --)
{
swapReference(a, 0, i); //deleteMax
percolateDown(a, 0, i);
}
}
/**
* @param n the logical size of the binary heap
*/
private static <T extends Comparable<? super T>> void percolateDown(T[] a, int i, int n)
{
int child;
T temp;
for(temp = a[i]; leftChild(i) < n; i = child)
{
child = leftChild(i);
if (a[child].compareTo(a[child + 1]) < 0 && child != n - 1)//if child = n-1 ,then child++ will overflow
{
child++;
}
if (temp.compareTo(a[child]) < 0)
{
a[i] = a[child];
}
}
a[i] = temp;
}
/**
* the array is from index 0 which is different from binary heap.
*/
private static int leftChild(int i)
{
return 2 * i + 1;
}
private static <T extends Comparable<? super T>> void swapReference(T[] a, int i, int j)
{
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}
交换排序:
1.冒泡排序
public static <T extends Comparable<? super T>> void bubbleSort(T[] a)
{
for(int i = 0; i < a.length - 1; i++)
{
for(int j = 0; j < a.length - i - 1; j++)
{
if (a[j + 1].compareTo(a[j]) < 0)
{
T temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
2.快速排序
private static final int CUTOFF = 10;
private static <T extends Comparable<? super T>> void quicksort(T[] a,
int left, int right)
{
if (left + CUTOFF <= right)
{
T pivot = median3(a, left, right);
// Begin partitioning
int i = left, j = right - 1;
for (;;)
{
while (a[++i].compareTo(pivot) < 0){}
while (a[--j].compareTo(pivot) > 0){}
if (i < j)
{
swapReference(a, i, j);
}
else
{
break;
}
}
swapReference(a, i, right - 1); // restore pivot
// recursive calls
quicksort(a, left, i - 1);
quicksort(a, i + 1, right);
}
else //Do an insertion sort on the subarray.
{
InsertionSort.insertionSort(a);
}
}
public static <T extends Comparable<? super T>> T median3(T[] a, int left,
int right)
{
int center = (left + right) / 2;
if (a[center].compareTo(a[left]) < 0)
{
swapReference(a, left, center);
}
if (a[right].compareTo(a[left]) < 0)
{
swapReference(a, left, right);
}
if (a[right].compareTo(a[center]) < 0)
{
swapReference(a, center, right);
}
swapReference(a, center, right - 1);
return a[right - 1];
}
public static <T extends Comparable<? super T>> void quickSort(T[] a)
{
quicksort(a, 0, a.length - 1);
}
private static <T extends Comparable<? super T>> void swapReference(T[] a,
int i, int j)
{
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}