基本排序算法
leetcode: 排序算法
冒泡排序
- 基础版排序:
public class BubbleSort {
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
- 优化版排序:
public class BubbleSort {
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
boolean flag = true;
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = false;
}
}
if (flag) break;
}
}
}
快速排序
public class QuickSort {
public void sort(int[] arr) {
quickSort(arr, 0, arr.length - 1);
}
public void quickSort(int[] arr, int begin, int end) {
if (begin >= end) return;
int pivot = arr[begin];
int left = begin;
int right = end;
while (left < right) {
while (left < right && arr[right] >= pivot) right--;
arr[left] = arr[right];
while (left < right && arr[left] <= pivot) left++;
arr[right] = arr[left];
}
arr[left] = pivot;
quickSort(arr, begin, left - 1);
quickSort(arr, left + 1, end);
}
}
选择排序
public class SimpleSelectSort {
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int maxIndex = 0;
for (int j = 0; j < arr.length - i; j++) {
if (arr[j] > arr[maxIndex]) maxIndex = j;
}
int temp = arr[maxIndex];
arr[maxIndex] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
}
}
插入排序
归并排序
public class MergeSort {
public void sort(int[] arr) {
mergeSort(arr, 0, arr.length - 1);
}
private void mergeSort(int[] arr, int begin, int end) {
if (begin >= end) return;
int middle = (begin + end) >> 1;
mergeSort(arr, begin, middle);
mergeSort(arr, middle + 1, end);
merge(arr, begin, middle, end);
}
private void merge(int[] arr, int begin, int mid, int end) {
int[] temp = new int[end - begin + 1];
int i = begin;
int j = mid + 1;
int index = 0;
while (i <= mid && j <= end) {
if (arr[i] <= arr[j])
temp[index++] = arr[i++];
else
temp[index++] = arr[j++];
}
while (i <= mid) temp[index++] = arr[i++];
while (j <= end) temp[index++] = arr[j++];
for (int num : temp) arr[begin++] = num;
}
}
public class MergeSort {
public void sort(int[] arr) {
mergeSort(arr, 0, arr.length - 1);
}
private void mergeSort(int[] arr, int begin, int end) {
if (begin >= end) return;
int middle = (begin + end) >> 1;
mergeSort(arr, begin, middle);
mergeSort(arr, middle + 1, end);
merge(arr, begin, middle, end);
}
private void merge(int[] arr, int begin, int mid, int end) {
int[] temp = new int[end - begin + 1];
int i = begin;
int j = mid + 1;
int index = 0;
while (i <= mid || j <= end) {
if (i > mid) temp[index++] = arr[j++];
else if (j > end) temp[index++] = arr[i++];
else if (arr[i] <= arr[j]) temp[index++] = arr[i++];
else temp[index++] = arr[j++];
}
for (int num : temp) arr[begin++] = num;
}
}
排序算法 | 平均时间复杂度 | 最好时间复杂度 | 最坏时间复杂度 | 稳定性 |
---|---|---|---|---|
冒泡排序 | O(n2) | O(n2) / O(n) | O(n2) | 稳定 |
快速排序 | O(nlogn) | O(nlogn) | O(n2) | 不稳定 |
选择排序 | O(n2) | O(n2) | O(n2) | 不稳定 |
归并排序 | O(nlogn) | O(nlogn) | O(nlogn) | 稳定 |