插入排序的实现(用的是泛型)
package com.sort.cc;
import java.util.Arrays;
/**
*
* @author SunnyBoy
* @version Time:2017年7月24日 下午3:20:29
*/
public class InsertionSort {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
Comparable[] a = { 1, 3, 5, 7, 9, 2, 4, 6, 8 };
insertionSort(a);
}
public static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a) {
for (int p = 1; p < a.length; p++) {
AnyType tmp = a[p];
int j = p;
for (; j > 0 && tmp.compareTo(a[j - 1]) < 0; j--)
a[j] = a[j - 1];
a[j] = tmp;
System.out.println(Arrays.toString(a));
}
}
}
希尔排序的实现(用的是泛型,性能极大地依赖于增量序列,而且欠缺一个挑战性地分析,性能不佳,但是编码简答,工作时间处于良好地亚平方时间里)
package com.sort.cc;
import java.util.Arrays;
/**
*
* @author SunnyBoy
* @version Time:2017年7月24日 下午3:21:12
*/
public class ShellSort {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
Comparable[] a = { 1, 3, 5, 7, 9, 2, 4, 6, 8 };
shellSort(a);
}
private static <AnyType extends Comparable<? super AnyType>> void shellSort(AnyType[] a) {
for (int gap = a.length / 2; gap > 0; gap = gap == 2 ? 1 : (int) (gap / 2.2))
for (int i = gap; i < a.length; i++) {
AnyType tmp = a[i];
int j = i;
for (; j >= gap && tmp.compareTo(a[j - gap]) < 0; j -= gap)
a[j] = a[j - gap];
a[j] = tmp;
}
System.out.println(Arrays.toString(a));
}
}
归并排序的实现(用的是泛型,在这里,主要是拆分成两个数组,作归并实现的过程,也就是大家熟悉的分治算法)
package com.sort.cc;
import java.util.Arrays;
/**
*
* @author SunnyBoy
* @version Time:2017年7月24日 下午2:32:53
* 归并排序将数组的前半部分作为A,后半部分作为B,将临时数组作为C。merge函数中实现了归并过程,临时数组最后被拷贝回原始数组
*/
public class MergeSort {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
Comparable[] a = { 1, 3, 5, 7, 9, 2, 4, 6, 8 };
mergeSort(a);
System.out.println(Arrays.toString(a));
}
public static <AnyType extends Comparable<? super AnyType>> void mergeSort(AnyType[] a) {
@SuppressWarnings("unchecked")
AnyType[] tmpArray = (AnyType[]) new Comparable[a.length];
mergeSort(a, tmpArray, 0, a.length - 1);
}
private static <AnyType extends Comparable<? super AnyType>> void mergeSort(AnyType[] a, AnyType[] tmpArray,
int left, int right) {
if (left < right) {
int center = (left + right) / 2;
mergeSort(a, tmpArray, left, center);
mergeSort(a, tmpArray, center + 1, right);
merge(a, tmpArray, left, center + 1, right);
}
}
/**
* Internal method that merges two sorted halves of a subarray.
* @param a an array of Comparable items.
* @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 <AnyType extends Comparable<? super AnyType>> void merge(AnyType[] a, AnyType[] 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 (a[leftPos].compareTo(a[rightPos]) <= 0)
tmpArray[tmpPos++] = a[leftPos++];
else
tmpArray[tmpPos++] = a[rightPos++];
while (leftPos <= leftEnd)
tmpArray[tmpPos++] = a[leftPos++];
while (rightPos <= rightEnd)
tmpArray[tmpPos++] = a[rightPos++];
//Copy tmpArray back
for (int i = 0; i < numElements; i++, rightEnd--)
a[rightEnd] = tmpArray[rightEnd];
}
}
快速排序的实现(用的也是泛型,一个是适当实现时速度很快的分治算法)
package com.sort.cc;
import java.util.Arrays;
/**
*
* @author SunnyBoy
* @version Time:2017年7月24日 下午3:10:58
* 快速排序的内层循环是紧凑而有效的
*/
public class QuickSort {
private static final int CUTOFF = 3;
@SuppressWarnings("unchecked")
public static void main(String[] args) {
@SuppressWarnings("rawtypes")
Comparable[] a = {1,3,5,7,9,2,4,6,8};
quicksort(a);
System.out.println(Arrays.toString(a));
}
public static <AnyType extends Comparable<? super AnyType>> void quicksort(AnyType[] a) {
quicksort(a, 0, a.length - 1);
}
private static <AnyType extends Comparable<? super AnyType>> void quicksort(AnyType[] a, int low, int high) {
if (low + CUTOFF > high)
insertionSort(a);
else {
int middle = (low + high) / 2;
if (a[middle].compareTo(a[low]) < 0)
swapReferences(a, low, middle);
if (a[high].compareTo(a[low]) < 0)
swapReferences(a, low, high);
if (a[high].compareTo(a[middle]) < 0)
swapReferences(a, middle, high);
swapReferences(a, middle, high - 1);
AnyType pivot = a[high - 1];
int i, j;
for (i = low, j = high - 1;;) {
while (a[++i].compareTo(pivot) < 0)
;
while (pivot.compareTo(a[--j]) < 0)
;
if (i >= j)
break;
swapReferences(a, i, j);
}
swapReferences(a, i, high - 1);
quicksort(a, low, i - 1);
quicksort(a, i + 1, high);
}
}
public static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a) {
for (int p = 1; p < a.length; p++) {
AnyType tmp = a[p];
int j = p;
for (; j > 0 && tmp.compareTo(a[j - 1]) < 0; j--)
a[j] = a[j - 1];
a[j] = tmp;
}
}
private static <AnyType extends Comparable<? super AnyType>> void swapReferences(AnyType[] a, int i, int j) {
AnyType tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}