八種排序演算法
阿新 • • 發佈:2020-07-15
https://juejin.im/post/5b95da8a5188255c775d8124#heading-4
1. 氣泡排序Bubble Sort
每次冒出一個最大的到最後一位
public void bbsort(int[] arr){ int le = arr.length; for(int i = 0; i < le - 1; i++){ for(int j = 0; j < le - i - 1; j++){ if(arr[j] > arr[j+1]) swap(arr, j, j+1); } } }
public void swapbycount(int[] arr, int a, int b){ arr[a] = arr[a] + arr[b]; arr[b] = arr[a] - arr[b]; arr[a] = arr[a] - arr[b]; }
還可以byBit
public static void swapByBitOperation(int[] array, int i, int j) { array[i] = array[i]^array[j]; array[j] = array[i]^array[j]; //array[i]^array[j]^array[j]=array[i] array[i] = array[i]^array[j]; //array[i]^array[j]^array[i]=array[j] } 作者:foofoo 連結:https://juejin.im/post/5b95da8a5188255c775d8124 來源:掘金 著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。
2. 快速排序 quick sort
3. 插入排序 insertion sort
從第二個數開始,每次把當前的數放在合適的位置(通過和前面的數比較)
publicvoid insertionSort(int[ ] arr){ int le = arr.length; for(int i = 1; i < le; i++){ int j = i-1; int tmp = arr[i]; while(j >= 0 && arr[i] < arr[j]){ a[j+1] = a[j]; j--; } a[j+1] = tmp; } }
4. 希爾排序ShellSort
改進版的插入排序
每次用gap來對陣列分組,在組內進行插入排序,gap by default == n / 2, 當gap == 1時排序完成就結束了
https://www.geeksforgeeks.org/shellsort/
int sort(int arr[]) { int n = arr.length; // Start with a big gap, then reduce the gap for (int gap = n/2; gap > 0; gap /= 2) { // Do a gapped insertion sort for this gap size. // The first gap elements a[0..gap-1] are already // in gapped order keep adding one more element // until the entire array is gap sorted for (int i = gap; i < n; i += 1) { // add a[i] to the elements that have been gap // sorted save a[i] in temp and make a hole at // position i int temp = arr[i]; // shift earlier gap-sorted elements up until // the correct location for a[i] is found int j; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) arr[j] = arr[j - gap]; // put temp (the original a[i]) in its correct // location arr[j] = temp; } } return 0; }
5. 選擇排序 selection sort
每次把當前數與後面所有的數對比並替換成最小的數
public class SelectSort { public static void sort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int min = i; for (int j = i+1; j < arr.length; j ++) { //選出之後待排序中值最小的位置 if (arr[j] < arr[min]) { min = j; } } if (min != i) { arr[min] = arr[i] + arr[min]; arr[i] = arr[min] - arr[i]; arr[min] = arr[min] - arr[i]; } } }
6. 歸併排序 merge sort
用了divide and conquer分治法的思想,對陣列遞迴呼叫sort 和 merge
merge中,先建立兩個臨時陣列分別存放mid左邊和mid右邊的已經排好的數列,然後合併時,有一方結束就先暫時結束,然後再檢查有沒有剩下的數加進去就可以了
public static void sort(int[] arr, int l, int r) { if(l < r) { int mid = l + (r - l) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(l, r, mid, arr); } } public static void merge(int left, int right, int mid, int[] arr) { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1]; int[] R = new int[n2]; for(int i = 0; i < L.length; i++) L[i] = arr[left + i]; for(int i = 0; i < R.length; i++) R[i] = arr[mid + 1 + i]; int k = left; int i = 0, j = 0; while(i < n1 && j < n2) { if(L[i] <= R[j]){ arr[k++] = L[i++]; } else arr[k++] = R[j++]; } while(i < n1) arr[k++] = L[i++]; while(j < n2) arr[k++] = R[j++]; }
7. 基數排序
8. 堆排序 heapsort