冒泡排序(Bubble Sort)
- 优点:易于理解和实现。
- 缺点:效率较低,时间复杂度较高。
- 时间复杂度:平均情况和最坏情况下的时间复杂度均为 O(n^2)。
- 空间复杂度:O(1)。
- 稳定性:稳定
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
插入排序(Insertion Sort)
- 优点:对于小型数据集或已接近排序状态的数据集效率较高。
- 缺点:效率较低,在大型数据集上性能不佳。
- 时间复杂度:平均情况和最坏情况下的时间复杂度均为 O(n^2)。
- 空间复杂度:O(1)。
- 稳定性:稳定
public class InsertionSort {
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
}
选择排序(Selection Sort)
- 优点:实现简单,不需要额外的内存空间。
- 缺点:效率较低,在大型数据集上性能不佳。
- 时间复杂度:平均情况和最坏情况下的时间复杂度均为 O(n^2)。
- 空间复杂度:O(1)。
- 稳定性:不稳定
public class SelectionSort {
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
快速排序(QuickSort)
- 优点:在平均情况下具有较高的效率,是最常用的排序算法之一。
- 缺点:对于小型数据集或已近乎有序的数据集效率不佳。
- 时间复杂度:平均情况下的时间复杂度为 O(n log n),最坏情况下为 O(n^2)。
- 空间复杂度:O(log n)。
- 稳定性:不稳定
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
private static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
}
希尔排序(Shell Sort)
- 优点:对于中型数据集有较高的效率。
- 缺点:不稳定,无法保证性能。
- 时间复杂度:平均情况下的时间复杂度为 O(n log n)。
- 空间复杂度:O(1)。
- 稳定性:不稳定
public class ShellSort {
public static void shellSort(int[] arr) {
int n = arr.length;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
}
}
堆排序(Heap Sort)
- 优点:在大型数据集上具有较高的效率。
- 缺点:不稳定,无法保证性能。
- 时间复杂度:平均情况下的时间复杂度为 O(n log n)。
- 空间复杂度:O(1)。
- 稳定性:不稳定
public class HeapSort {
public static void heapSort(int[] arr) {
int n = arr.length;
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
private static void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right
归并排序(Merge Sort)
- 优点:在任何情况下都具有稳定的 O(n log n) 时间复杂度。对于链表等非随机访问数据结构也适用。
- 缺点:需要额外的空间来存储中间结果。
- 时间复杂度:平均情况下的时间复杂度为 O(n log n)。
- 空间复杂度:O(n)。
- 稳定性:稳定
public class MergeSort {
public static void mergeSort(int[] arr, int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
private static void merge(int[] arr, int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int[] L = new int[n1];
int[] R = new int[n2];
System.arraycopy(arr, l, L, 0, n1);
System.arraycopy(arr, m + 1, R, 0, n2);
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
}
基数排序(Redix Sort)
- 优点:适用于整数或字符串等特定类型的数据。
- 缺点:对于数据范围过大或者字符串长度不一的数据集效率较低。
- 时间复杂度:平均情况下的时间复杂度为 O(nk),其中 k 是最大的数字的位数。
- 空间复杂度:O(n + k)。
- 稳定性:稳定
public class RadixSort {
public static void radixSort(int[] arr) {
int max = Arrays.stream(arr).max().getAsInt();
for (int exp = 1; max / exp > 0; exp *= 10) {
countSort(arr, exp);
}
}
private static void countSort(int[] arr, int exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10];
Arrays.fill(count, 0);
for (int j : arr) {
count[(j / exp) % 10]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
System.arraycopy(output, 0, arr, 0, n);
}
}
总结