0%
整理常见的排序方式,尽量挑几个达到手写
冒泡排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
private static int[] bubbleSort(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; }
|
选择排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
private static int[] selectSort(int[] arr) { for (int i = 0; i < arr.length; i++) { int index = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[index]) { index = j; } } if (index != i) { int temp = arr[index]; arr[index] = arr[i]; arr[i] = temp; } } return arr; }
|
插入排序
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
|
private static int[] insertSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int j; if (arr[i] < arr[i - 1]) { int temp = arr[i]; for (j = i - 1; j >= 0 && temp < arr[j]; j--) { arr[j + 1] = arr[j]; } arr[j + 1] = temp; } } return arr; }
public static void insertSort(int[] arr) { int temp; for (int i = 1; i < arr.length; i++) { for (int j = i; j > 0; j--) { if (arr[j - 1] > arr[j]) { temp = arr[j - 1]; arr[j - 1] = arr[j]; arr[j] = temp; } } } }
|
希尔排序
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 int[] shellSort(int[] arr) { int increasement = arr.length; int i, j, k; do { increasement = increasement / 3 + 1; for (i = 0; i < increasement; i++) { for (j = i + increasement; j < arr.length; j += increasement) { if (arr[j] < arr[j - increasement]) { int temp = arr[j]; for (k = j - increasement; k >= 0 && temp < arr[k]; k -= increasement) { arr[k + increasement] = arr[k]; } arr[k + increasement] = temp; } } } } while (increasement > 1); return arr; }
|
快速排序
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
|
private static void quickSort(int[] arr, int start, int end) { if (start >= end) return; int i = start; int j = end; int baseval = arr[start]; while (i < j) { while (i < j && arr[j] >= baseval) { j--; } if (i < j) { arr[i] = arr[j]; i++; } while (i < j && arr[i] < baseval) { i++; } if (i < j) { arr[j] = arr[i]; j--; } } arr[i] = baseval; quickSort(arr, start, i - 1); quickSort(arr, i + 1, end); }
|
归并排序
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
|
private static void mergeSort(int arr[], int start, int end, int... temp) { if (start >= end) return; int mid = (start + end) / 2; mergeSort(arr, start, mid, temp); mergeSort(arr, mid + 1, end, temp); int length = 0; int i_start = start; int i_end = mid; int j_start = mid + 1; int j_end = end; while (i_start <= i_end && j_start <= j_end) { if (arr[i_start] < arr[j_start]) { temp[length] = arr[i_start]; length++; i_start++; } else { temp[length] = arr[j_start]; length++; j_start++; } } while (i_start <= i_end) { temp[length] = arr[i_start]; i_start++; length++; } while (j_start <= j_end) { temp[length] = arr[j_start]; length++; j_start++; } for (int i = 0; i < length; i++) { arr[start + i] = temp[i]; } }
|
堆排序
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
| private static void heapAdjust(int arr[], int i, int length) { int max = i; int lchild = i * 2 + 1; int rchild = i * 2 + 2; if (lchild < length && arr[lchild] > arr[max]) { max = lchild; } if (rchild < length && arr[rchild] > arr[max]) { max = rchild; } if (max != i) { int temp; temp = arr[i]; arr[i] = arr[max]; arr[max] = temp; heapAdjust(arr, max, length); } }
private static void heapSort(int arr[], int length) { for (int i = length / 2 - 1; i >= 0; i--) { heapAdjust(arr, i, length); } for (int i = length - 1; i >= 0; i--) { int temp; temp = arr[i]; arr[i] = arr[0]; arr[0] = temp; heapAdjust(arr, 0, i); } }
|