排序算法java示例
package test;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Function;
public class SortTest {
public static void main(String[] args) {
int[] temp = new int[10];
for (int i = 0; i < 10; i++) {
temp[i] = (int) (Math.random() * 199) - 190;
}
System.out.println("希尔排序");
doSort(temp, SortTest::shellSort);
System.out.println("插入排序");
doSort(temp, SortTest::insertSort);
System.out.println("选择排序");
doSort(temp, SortTest::selectSort);
System.out.println("选择排序优化");
doSort(temp, SortTest::selectSortMinMax);
System.out.println("冒泡排序");
doSort(temp, SortTest::bubbleSort);
System.out.println("堆排序");
doSort(temp, SortTest::heapSort);
System.out.println("快速排序");
doSort(temp, (Consumer<int[]>) SortTest::quickSort);
System.out.println("合并排序");
doSort(temp, SortTest::doMergeSort);
System.out.println("计数排序");
doSort(temp, SortTest::countingSort);
System.out.println("桶排序");
doSort(temp, SortTest::bucketSort);
System.out.println("基数排序基于桶排序");
doSort(temp, SortTest::radixSortByBucket);
System.out.println("基数排序基于计数排序");
doSort(temp,SortTest::radixSortByCounting);
}
private static void doSort(int[] temp, Consumer<int[]> consumer) {
int[] ints = Arrays.copyOf(temp, temp.length);
System.out.println("原数组:");
Arrays.stream(temp).forEach(i -> System.out.print(i + " "));
Long start = System.currentTimeMillis();
consumer.accept(ints);
System.out.println("耗时:" + (System.currentTimeMillis() - start));
Arrays.stream(ints).forEach(i -> System.out.print(i + " "));
System.out.println();
}
private static void doSort(int[] temp, Function<int[], int[]> function) {
int[] ints = Arrays.copyOf(temp, temp.length);
System.out.println("原数组:");
Arrays.stream(temp).forEach(i -> System.out.print(i + " "));
Long start = System.currentTimeMillis();
ints = function.apply(ints);
System.out.println("耗时:" + (System.currentTimeMillis() - start));
Arrays.stream(ints).forEach(i -> System.out.print(i + " "));
System.out.println();
}
private static void insertSort(int[] ints) {
for (int index = 1; index < ints.length; index++) {
int i = ints[index];
int temp = index - 1;
for (; temp >= 0; temp--) {
if (i < ints[temp]) {
ints[temp + 1] = ints[temp];
} else {
break;
}
}
ints[temp + 1] = i;
}
}
private static void shellSort(int[] ints) {
for (int step = ints.length / 2; step > 0; step /= 2) {
for (int index = step; index < ints.length; index++) {
int i = ints[index];
int temp = index - step;
for (; temp >= 0; temp -= step) {
if (i < ints[temp]) {
ints[temp + step] = ints[temp];
} else {
break;
}
}
ints[temp + step] = i;
}
}
}
private static void selectSort(int[] ints) {
int length = ints.length;
for (int i = 0; i < length - 1; i++) {
int min = i;
for (int j = i + 1; j < length; j++) {
if (ints[min] > ints[j]) {
min = j;
}
}
int temp = ints[min];
ints[min] = ints[i];
ints[i] = temp;
}
}
private static void selectSortMinMax(int[] ints) {
int length = ints.length;
for (int i = 0; i < length / 2; i++) {
int min = i;
int max = length - i - 1;
for (int j = i + 1; j < length - i; j++) {
if (ints[min] > ints[j]) {
min = j;
} else if (ints[max] < ints[j]) {
max = j;
}
}
int temp;
temp = ints[i];
ints[i] = ints[min];
ints[min] = temp;
if (min == max) {
max = i;
}
temp = ints[length - i - 1];
ints[length - i - 1] = ints[max];
ints[max] = temp;
}
}
private static void bubbleSort(int[] ints) {
int length = ints.length;
for (int j = length; j >= 0; j--) {
int flag = 0;
for (int i = 0; i < j - 1; i++) {
if (ints[i] > ints[i + 1]) {
flag = 1;
int temp = ints[i];
ints[i] = ints[i + 1];
ints[i + 1] = temp;
}
}
if (flag == 0) {
break;
}
}
}
private static void heapSort(int[] ints) {
int length = ints.length;
for (int root = (length - 2) / 2; root >= 0; root--) {
adjustDown(ints, length, root);
}
while (length > 1) {
int temp = ints[0];
ints[0] = ints[length - 1];
ints[length - 1] = temp;
length--;
adjustDown(ints, length, 0);
}
}
private static void adjustDown(int[] ints, int length, int root) {
for (int i = root; i < length; ) {
int max = i * 2 + 1;
if (max >= length) {
return;
}
if (i * 2 + 2 < length && ints[i * 2 + 1] < ints[i * 2 + 2]) {
max = i * 2 + 2;
}
if (ints[i] < ints[max]) {
int temp = ints[i];
ints[i] = ints[max];
ints[max] = temp;
} else {
return;
}
i = max;
}
}
private static void quickSort(int[] ints) {
if (ints.length <= 1) {
return;
}
quickSort(ints, 0, 0, ints.length - 1);
}
private static void quickSort(int[] ints, int key, int left, int right) {
if (left >= right) {
return;
}
while (true) {
while (ints[right] >= ints[key] && right > left) {
right--;
}
while (ints[left] <= ints[key] && left < right) {
left++;
}
if (right == left) {
break;
}
int temp = ints[left];
ints[left] = ints[right];
ints[right] = temp;
}
int temp = ints[left];
ints[left] = ints[key];
ints[key] = temp;
quickSort(ints, key, key, left - 1);
quickSort(ints, left + 1, left + 1, ints.length - 1);
}
private static int[] doMergeSort(int[] ints) {
return mergeSort(ints);
}
private static int[] mergeSort(int[] ints) {
int length = ints.length;
if (length < 2) {
return ints;
}
int mid = length / 2;
int[] left = Arrays.copyOf(ints, mid);
int[] right = Arrays.copyOfRange(ints, mid, length);
int[] result = merge(mergeSort(left), mergeSort(right));
return result;
}
private static int[] merge(int[] left, int[] right) {
int leftLength = left.length;
int rightLength = right.length;
int[] result = new int[leftLength + rightLength];
int indexL = 0;
int indexR = 0;
int index = 0;
while (indexL < leftLength && indexR < rightLength) {
if (left[indexL] <= right[indexR]) {
result[index++] = left[indexL++];
} else {
result[index++] = right[indexR++];
}
}
while (indexL < leftLength) {
result[index++] = left[indexL++];
}
while (indexR < rightLength) {
result[index++] = right[indexR++];
}
return result;
}
private static void countingSort(int[] ints) {
int[] maxAndMin = findMaxAndMin(ints);
int max = maxAndMin[0];
int min = maxAndMin[1];
int[] count = new int[max - min + 1];
for (int i : ints) {
count[i - min]++;
}
int index = 0;
for (int i = 0; i < count.length; i++) {
int value = min + i;
while (count[i] > 0) {
ints[index++] = value;
count[i]--;
}
}
}
private static void bucketSort(int[] ints) {
int[] maxAndMin = findMaxAndMin(ints);
int max = maxAndMin[0];
int min = maxAndMin[1];
int bucketSize = 10;
int bucketCount = (max - min) / bucketSize + 1;
int[][] bucketArray = new int[bucketCount][0];
for (int i : ints) {
int index = (i - min) / bucketSize;
bucketArray[index] = append(bucketArray[index], i);
}
for (int[] bucket : bucketArray) {
if (bucket.length > 1) {
insertSort(bucket);
}
}
int index = 0;
for (int[] bucket : bucketArray) {
for (int i : bucket) {
ints[index++] = i;
}
}
}
private static int[] append(int[] ints, int i) {
int[] newInts = Arrays.copyOf(ints, ints.length + 1);
newInts[newInts.length - 1] = i;
return newInts;
}
private static int[] findMaxAndMin(int[] ints) {
int max = ints[0];
int min = ints[0];
for (int i : ints) {
if (max < i) {
max = i;
}
if (min > i) {
min = i;
}
}
return new int[]{max, min};
}
private static void radixSortByBucket(int[] ints) {
int[] maxAndMin = findMaxAndMin(ints);
int max = Math.abs(maxAndMin[0]);
int min = Math.abs(maxAndMin[1]);
max = max>min?max:min;
int length = 0;
while (max > 0) {
length++;
max /= 10;
}
int dev = 1;
for (int i = 0; i < length; i++, dev *= 10) {
int index = 0;
int[][] bucketArray = new int[19][0];
for (int j : ints) {
index = j / dev % 10 + 9;
bucketArray[index] = append(bucketArray[index], j);
}
index = 0;
for (int[] bucket : bucketArray) {
for (int b : bucket) {
ints[index++] = b;
}
}
}
}
private static void radixSortByCounting(int[] ints) {
int[] maxAndMin = findMaxAndMin(ints);
int max = Math.abs(maxAndMin[0]);
int min = Math.abs(maxAndMin[1]);
max = max>min?max:min;
int length = 0;
while (max > 0) {
length++;
max /= 10;
}
int dev = 1;
for (int i = 0; i < length; i++, dev *= 10) {
int[] count = new int[19];
for (int j : ints) {
count[j / dev % 10 + 9]++;
}
int pre=count[0];
count[0]=0;
for(int index=1;index<count.length;index++){
int temp=count[index];
count[index]=pre+count[index-1];
pre=temp;
}
int[] temp = Arrays.copyOf(ints,ints.length);
for(int t:temp){
int index = count[t / dev % 10 + 9]++;
ints[index]=t;
}
}
}
}