十大排序算法
十大排序算法 冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序、计数排序、桶排序、基数排序。
字典:
- n:数据规模
- k:桶的大小(整数范围)
- 稳定性:排序后两个相等元素的顺序和排序之前是否相同
java
// 交换元素
public static void swap(int[] ints, int m, int n) {
int temp = ints[m];
ints[m] = ints[n];
ints[n] = temp;
}
1、冒泡排序
java
// 冒泡排序
public static int[] bubbleSort(int[] ints) {
for (int i = 0; i < ints.length - 1; i++) {
for (int j = 0; j < ints.length - 1 - i; j++) {
if (ints[j] > ints[j + 1]) {
swap(ints, j, j + 1);
}
}
}
return ints;
}
// 鸡尾酒排序(冒泡的升级版)
public static int[] cocktailSort(int[] ints) {
for (int i = 0; i < ints.length / 2; i++) {
for (int j = i; j < ints.length - 1 - i; j++) {
if (ints[j] > ints[j + 1]) {
swap(ints, j, j + 1);
}
}
for (int j = ints.length - 1 - (i + 1); j > i; j--) {
if (ints[j] < ints[j - 1]) {
swap(ints, j, j - 1);
}
}
}
return ints;
}
2、选择排序
java
// 选择排序
public static int[] selectionSort(int[] ints) {
for (int i = 0; i < ints.length - 1; i++) {
int minIndex = i;
for (int j = i; j < ints.length - 1; j++) {
if (ints[j + 1] < ints[minIndex]) {
minIndex = j + 1;
}
}
if (minIndex != i) {
swap(ints, i, minIndex);
}
}
return ints;
}
3、插入排序
java
// 插入排序
public static int[] insertionSort(int[] ints) {
for (int i = 1; i < ints.length; i++) {
for (int j = i; j > 0; j--) {
if (ints[j] < ints[j - 1]) {
swap(ints, j - 1, j);
}
}
}
return ints;
}
4、希尔排序
java
// 希尔排序(插入的升级版)
public static int[] shellSort(int[] ints) {
for (int step = ints.length / 2; step > 0; step /= 2) {
for (int i = step; i < ints.length; i++) {
for (int j = i; j >= step; j -= step) {
if (ints[j] < ints[j - step]) {
swap(ints, j -step, j);
}
}
}
}
return ints;
}
5、归并排序
java
// 归并排序
public static int[] mergeSort(int[] ints, int startIndex, int endIndex) {
if (startIndex == endIndex) {
return new int[] { ints[startIndex] };
}
int midIndex = startIndex + (endIndex - startIndex) / 2;
int[] left = mergeSort(ints, startIndex, midIndex);
int[] right = mergeSort(ints, midIndex + 1, endIndex);
int[] temp = new int[left.length + right.length];
int m = 0, n = 0, index = 0;
while (m < left.length && n < right.length) {
temp[index++] = left[m] < right[n] ? left[m++] : right[n++];
}
while (m < left.length) {
temp[index++] = left[m++];
}
while (n < right.length) {
temp[index++] = right[n++];
}
return temp;
}
6、快速排序
java
// 快速排序
public static void quickSort(int[] ints, int startIndex, int endIndex) {
if (startIndex > endIndex) {
return;
}
int key = ints[startIndex];
int i = startIndex, j = endIndex;
while (i < j) {
while (i < j && ints[j] > key) {
j--;
}
while (i < j && ints[i] < key) {
i++;
}
if (i < j) {
swap(ints, i, j);
}
}
ints[startIndex] = ints[i];
ints[i] = key;
quickSort(ints, startIndex, i - 1);
quickSort(ints, i + 1, endIndex);
}
7、堆排序
java
// 堆排序
public static int[] heapSort(int[] ints) {
for (int i = ints.length / 2 - 1; i >= 0; i--) {
adjustHeap(ints, i, ints.length);
}
for (int i = ints.length - 1; i > 0; i--) {
swap(ints, 0, i);
adjustHeap(ints, 0, i);
}
return ints;
}
public static void adjustHeap(int[] ints, int index, int length) {
int maxIndex = index;
int leftIndex = 2 * index + 1;
int rightIndex = 2 * index + 2;
if (leftIndex < length && ints[leftIndex] > ints[maxIndex]) {
maxIndex = leftIndex;
}
if (rightIndex < length && ints[rightIndex] > ints[maxIndex]) {
maxIndex = rightIndex;
}
if (maxIndex != index) {
swap(ints, index, maxIndex);
adjustHeap(ints, maxIndex, length);
}
}
8、计数排序
java
// 计数排序
public static int[] countingSort(int[] ints) {
int max = ints[0], min = ints[0];
for (int item : ints) {
max = Math.max(max, item);
min = Math.min(min, item);
}
int k = max - min + 1;
int[] temp = new int[k];
for (int item : ints) {
temp[item - min]++;
}
int index = 0;
for (int i = 0; i < temp.length; i++) {
while (temp[i] > 0) {
ints[index++] = i + min;
temp[i]--;
}
}
return ints;
}
9、桶排序
java
// 桶排序
public static int[] bucketSort(int[] ints, int capacity) {
int max = ints[0], min = ints[0];
for (int item : ints) {
max = Math.max(max, item);
min = Math.min(min, item);
}
int k = (max - min) / capacity + 1;
int[][] temp = new int[k][0];
for (int item : ints) {
int bucketIndex = (item - min) / capacity;
int[] bucket = Arrays.copyOf(temp[bucketIndex], temp[bucketIndex].length + 1);
bucket[bucket.length - 1] = item;
temp[bucketIndex] = bucket;
}
int index = 0;
for (int[] bucket : temp) {
if (bucket.length == 0) {
continue;
}
bubbleSort(bucket);
for (int item : bucket) {
ints[index++] = item;
}
}
return ints;
}
10、基数排序
java
// 基数排序
public static int[] radixSort(int[] ints) {
int maxAbs = Math.abs(ints[0]);
for (int item : ints) {
int abs = Math.abs(item);
maxAbs = Math.max(maxAbs, abs);
}
int maxDigit = 0;
while (maxAbs != 0) {
maxAbs /= 10;
maxDigit++;
}
for (int i = 0, m = 10, n = 1; i < maxDigit; i++, m *= 10, n *= 10) {
int[][] temp = new int[10 * 2][0];
for (int item : ints) {
int bucketIndex = (item % m) / n + 10;
int[] bucket = Arrays.copyOf(temp[bucketIndex], temp[bucketIndex].length + 1);
bucket[bucket.length - 1] = item;
temp[bucketIndex] = bucket;
}
int index = 0;
for (int[] bucket : temp) {
if (bucket.length == 0) {
continue;
}
for (int item : bucket) {
ints[index++] = item;
}
}
}
return ints;
}
@XGLLHZ - 陈慧娴 《夜机》.mp3