Java实现排序
Java源文件地址:
百度云:http://pan.baidu.com/s/1qW6ygzU
提取码:dndr
交换函数:
public static void swap(int array[], int x, int y) {
int t = array[x];
array[x] = array[y];
array[y] = t;
}
一、直接插入:
public static void insertSort(int[] array) {
if (array == null || array.length <= 1)
return;
for (int i = 1; i < array.length; i++) {
for (int j = i - 1; j >= 0 && array[j] > array[j + 1]; j--) {
swap(array, j, j + 1);
}
}
}
二、希尔排序:
public static void shellSort(int[] array) {
if (array == null || array.length <= 1)
return;
int length = array.length, j;
for (int value = length >> 1; value > 0; value >>= 1) {
for (int i = value; i < length; i++) {
int temp = array[i];
for (j = i; j >= value && temp < array[j - value]; j -= value) {
array[j] = array[j - value];
}
array[j] = temp;
}
}
}
三、直接选择:
public static void chooseSort(int[] array) {
if (array == null || array.length <= 1)
return;
for (int i = 0; i < array.length; ++i) {
int min = i;
for (int j = i + 1; j < array.length; ++j) {
if (array[min] > array[j])
min = j;
}
if (i != min)
swap(array, min, i);
}
}
四、堆排序:
public static int left(int index) {
return 2 * index + 1;
}
public static int right(int index) {
return 2 * index + 2;
}
public static void maxHeapify(int[] array, int size, int index) {
int left = left(index);
int right = right(index);
int largest = index;
if (left < size && array[left] > array[index]) {
largest = left;
}
if (right < size && array[right] > array[largest]) {
largest = right;
}
if (largest != index) {
swap(array, index, largest);
maxHeapify(array, size, largest);
}
}
public static void buildMaxHeap(int[] array) {
for (int index = array.length / 2 - 1; index >= 0; --index) {
maxHeapify(array, array.length, index);
}
}
public static void heapSort(int[] array) {
if (array == null || array.length <= 1)
return;
int size = array.length;
buildMaxHeap(array);
for (int i = array.length - 1; i > 0; --i) {
swap(array, 0, i);
--size;
maxHeapify(array, size, 0);
}
}
五、冒泡排序:
public static void bubbleSort(int[] array) {
if (array == null || array.length <= 1)
return;
for (int i = 0; i < array.length; ++i) {
for (int j = 0; j < array.length - i - 1; ++j) {
if (array[j] > array[j + 1]) {
swap(array, j, j + 1);
}
}
}
}
六、快速排序:
public static void quickSort(int[] array) {
if (array == null || array.length <= 1)
return;
doQuickSort(array, 0, array.length - 1);
}
public static void doQuickSort(int[] array, int begin, int end) {
if (begin < end) {
int random = randomizedPartition(array, begin, end);
doQuickSort(array, begin, random - 1);
doQuickSort(array, random + 1, end);
}
}
public static int randomizedPartition(int array[], int begin, int end) {
int random = (int) (Math.random() * (end - begin + 1) + begin);
swap(array, end, random);
return partition(array, begin, end);
}
public static int partition(int[] array, int begin, int end) {
int value = array[end];
int index = begin - 1;
for (int i = begin; i < end; ++i) {
if (array[i] <= value) {
++index;
swap(array, index, i);
}
}
swap(array, index + 1, end);
return index + 1;
}
七、归并排序:
public static void mergeSort(int[] array) {
if (array == null || array.length <= 1)
return;
doMergeSort(array, 0, array.length - 1);
}
public static void doMergeSort(int[] array, int begin, int end) {
if (begin >= end)
return;
int separate = (begin + end) / 2;
doMergeSort(array, begin, separate);
doMergeSort(array, separate + 1, end);
doMergeArray(array, begin, separate, end);
}
public static void doMergeArray(int[] array, int begin, int separate,
int end) {
int[] tempArray = new int[end - begin + 1];
int i = begin;
int j = separate + 1;
int index = 0;
while (i <=separate && j <= end) {
if (array[i] < array[j]) {
tempArray[index++] = array[i++];
} else {
tempArray[index++] = array[j++];
}
}
while (i <= separate) {
tempArray[index++] = array[i++];
}
while (j <= end) {
tempArray[index++] = array[j++];
}
for (int k = 0; k < tempArray.length; ++k) {
array[begin + k] = tempArray[k];
}
}
八、基数排序:
public static void radixSort(int[] array) {
if (array == null || array.length <= 1)
return;
int max = array[0];
int i, bit;
for (i = 0; i < array.length; ++i) {
if (array[i] > max) {
max = array[i];
}
}
bit = 0;
while (max > 0) {
max /= 10;
++bit;
}
int[] temp = new int[array.length];
int[] count = new int[10];
int divide = 1;
for (i = 0; i < bit; ++i) {
System.arraycopy(array, 0, temp, 0, array.length);
Arrays.fill(count, 0);
for (int j = 0; j < array.length; ++j) {
int tempKey = (temp[j] / divide) % 10;
count[tempKey]++;
}
for (int j = 1; j < 10; j++) {
count[j] = count[j] + count[j - 1];
}
for (int j = array.length - 1; j >= 0; j--) {
int tempKey = (temp[j] / divide) % 10;
count[tempKey]--;
array[count[tempKey]] = temp[j];
}
divide *= 10;
}
}