常见排序算法详解
排序算法是计算机科学中最基础也是最重要的算法之一。本文将介绍几种常见的排序算法,分析它们的时间复杂度、空间复杂度以及适用场景,并提供Java实现代码。
1. 冒泡排序 (Bubble Sort)
算法原理
冒泡排序是一种简单的排序算法,它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。
时间复杂度
- 最好情况:O(n),当数组已经有序时
- 最坏情况:O(n²)
- 平均情况:O(n²)
空间复杂度
O(1)
Java实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public static void bubbleSort(int[] arr) { if (arr == null || arr.length < 2) { return; } int n = arr.length; boolean swapped; for (int i = 0; i < n - 1; i++) { swapped = false; 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; swapped = true; } } if (!swapped) { break; } } }
|
适用场景
冒泡排序适用于小数据集,或者对稳定性有较高要求的场景。但由于其较高的时间复杂度,不适合大规模数据排序。
2. 选择排序 (Selection Sort)
算法原理
选择排序是一种简单直观的排序算法。它的工作原理是每次从未排序部分找出最小的元素,放到已排序部分的末尾。
时间复杂度
- 最好情况:O(n²)
- 最坏情况:O(n²)
- 平均情况:O(n²)
空间复杂度
O(1)
Java实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public static void selectionSort(int[] arr) { if (arr == null || arr.length < 2) { return; } 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; } } if (minIndex != i) { int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } }
|
适用场景
选择排序同样适用于小数据集,且在内存空间有限的情况下比较有用,因为它的交换操作次数较少。
3. 插入排序 (Insertion Sort)
算法原理
插入排序的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。
时间复杂度
- 最好情况:O(n),当数组已经有序时
- 最坏情况:O(n²)
- 平均情况:O(n²)
空间复杂度
O(1)
Java实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public static void insertionSort(int[] arr) { if (arr == null || arr.length < 2) { return; } 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--; } arr[j + 1] = key; } }
|
适用场景
插入排序在对几乎已经排好序的数据操作时效率很高,也适用于数据规模较小的排序。另外,它是一种稳定的排序算法,保持相等元素的相对位置不变。
4. 快速排序 (Quick Sort)
算法原理
快速排序采用分治法策略,选择一个基准元素,将数组分为两个子数组,小于基准的放在左边,大于基准的放在右边,然后递归地对子数组进行排序。
时间复杂度
- 最好情况:O(n log n)
- 最坏情况:O(n²),当数组已经有序或逆序时
- 平均情况:O(n log n)
空间复杂度
O(log n),递归调用栈的空间
Java实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 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; }
|
适用场景
快速排序是实际应用中最常用的排序算法之一,适用于大规模数据排序,平均性能非常好。但在最坏情况下性能会降低,并且不是稳定排序。
5. 归并排序 (Merge Sort)
算法原理
归并排序也是一种分治算法。它将数组分成两半,递归地对它们进行排序,然后将两部分合并成一个已排序的数组。
时间复杂度
- 最好情况:O(n log n)
- 最坏情况:O(n log n)
- 平均情况:O(n log n)
空间复杂度
O(n),合并过程需要额外空间
Java实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public static void mergeSort(int[] arr, int left, int right) { if (left < right) { int mid = left + (right - left) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); merge(arr, left, mid, right); } }
private static void merge(int[] arr, int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1]; int[] R = new int[n2]; for (int i = 0; i < n1; i++) { L[i] = arr[left + i]; } for (int j = 0; j < n2; j++) { R[j] = arr[mid + 1 + j]; } int i = 0, j = 0; int k = left; 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++; } }
|
适用场景
归并排序是一种稳定的排序算法,适用于对大型数据集进行外部排序,以及对链表进行排序。它的主要缺点是需要额外的空间复杂度。
6. 堆排序 (Heap Sort)
算法原理
堆排序是利用堆这种数据结构所设计的一种排序算法。它首先将数组构建成一个最大堆,然后将堆顶元素与最后一个元素交换,调整剩余元素为最大堆,重复此过程。
时间复杂度
- 最好情况:O(n log n)
- 最坏情况:O(n log n)
- 平均情况:O(n log n)
空间复杂度
O(1)
Java实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| 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 = 2 * i + 2; if (left < n && arr[left] > arr[largest]) { largest = left; } if (right < n && arr[right] > arr[largest]) { largest = right; } if (largest != i) { int swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; heapify(arr, n, largest); } }
|
适用场景
堆排序适用于排序大数据集,特别是在内存有限的情况下。但它不是稳定排序,且在实际应用中,通常被快速排序所取代。
7. 基数排序 (Radix Sort)
算法原理
基数排序是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较。它是一种分配式排序算法。
时间复杂度
- O(d(n+k)),其中d是最大数字的位数,n是元素个数,k是数字范围(0-9)
空间复杂度
Java实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public static void radixSort(int[] arr) { if (arr == null || arr.length < 2) { return; } int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } 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 i = 0; i < n; i++) { count[(arr[i] / 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]--; } for (int i = 0; i < n; i++) { arr[i] = output[i]; } }
|
适用场景
基数排序适用于整数排序,特别是对于取值范围有限的整数。它是一种稳定排序,但需要额外的空间。
8. 计数排序 (Counting Sort)
算法原理
计数排序是一种适用于整数排序的算法,它的核心思想是统计每个元素在待排序集合中出现的次数,然后根据统计结果排序。
时间复杂度
空间复杂度
Java实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public static void countingSort(int[] arr) { if (arr == null || arr.length < 2) { return; } int max = arr[0], min = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } } int range = max - min + 1; int[] count = new int[range]; int[] output = new int[arr.length]; for (int i = 0; i < arr.length; i++) { count[arr[i] - min]++; } for (int i = 1; i < range; i++) { count[i] += count[i - 1]; } for (int i = arr.length - 1; i >= 0; i--) { output[count[arr[i] - min] - 1] = arr[i]; count[arr[i] - min]--; } for (int i = 0; i < arr.length; i++) { arr[i] = output[i]; } }
|
适用场景
计数排序适用于整数排序,尤其是当整数分布范围较小时效率很高,是一种稳定的排序算法。
排序算法的比较
排序算法 |
最好时间复杂度 |
平均时间复杂度 |
最坏时间复杂度 |
空间复杂度 |
稳定性 |
冒泡排序 |
O(n) |
O(n²) |
O(n²) |
O(1) |
稳定 |
选择排序 |
O(n²) |
O(n²) |
O(n²) |
O(1) |
不稳定 |
插入排序 |
O(n) |
O(n²) |
O(n²) |
O(1) |
稳定 |
快速排序 |
O(n log n) |
O(n log n) |
O(n²) |
O(log n) |
不稳定 |
归并排序 |
O(n log n) |
O(n log n) |
O(n log n) |
O(n) |
稳定 |
堆排序 |
O(n log n) |
O(n log n) |
O(n log n) |
O(1) |
不稳定 |
基数排序 |
O(d(n+k)) |
O(d(n+k)) |
O(d(n+k)) |
O(n+k) |
稳定 |
计数排序 |
O(n+k) |
O(n+k) |
O(n+k) |
O(n+k) |
稳定 |
总结
排序算法是算法设计与分析的基础,也是解决复杂问题的重要工具。在实际应用中,应根据数据特点、规模大小和稳定性要求等因素选择合适的排序算法。
- 对于小规模数据或几乎已排序的数据,插入排序通常是最佳选择
- 对于大规模随机数据,快速排序通常表现最好
- 对于对稳定性有要求的场景,可以考虑归并排序、插入排序等稳定算法
- 对于内存受限的场景,堆排序可能是更好的选择
理解这些排序算法的工作原理和性能特点,不仅有助于选择合适的算法解决问题,也能帮助我们设计出更高效的程序。