Java常用八種排序演算法
阿新 • • 發佈:2019-01-03
- 直接插入排序
/** * 1.直接插入排序(穩定) * 原理:將陣列分為無序區和有序區兩個區,然後不斷將無序區的第一個元素按大小順序插入到有序區中去,最終將所有無序區元素都移動到有序區完成排序。 * 要點:設立哨兵,作為臨時儲存和判斷陣列邊界之用。 * */ public class InsertSort { // 排序原始資料 private static final int[] NUMBERS = {62, 99, 98, 54, 56, 17, 18, 23, 34, 15}; public static void insertSort(int[] array) { // 從頭部第一個當做已經排好序的,把後面的一個一個插入到已經排好序的列表中 for (int i = 1; i < array.length; i++) { int temp = array[i];// temp為待插入的值 int j; for (j = i - 1; j >= 0 && array[j] > temp; j--) { // 將大於temp的值整體後移一個單位 array[j + 1] = array[j]; } array[j + 1] = temp; } System.out.println("插入排序:" + Arrays.toString(array)); } public static void main(String[] args) { InsertSort.insertSort(NUMBERS); } }
- 希爾排序
/** * 2.希爾排序 * 原理:又稱縮小增量排序。先將序列按增量劃分為元素個數相同的若干組,使用直接插入排序法進行排序,然後不斷縮小增量直至為1,最後使用直接插入排序完成排序。 * 要點:增量的選擇以及排序最終以1為增量進行排序結束。 * */ public class ShellSort { // 排序原始資料 private static final int[] NUMBERS = {62, 99, 98, 54, 56, 17, 18, 23, 34, 15}; public static void shellSort(int[] array) { int len = array.length; while (len != 0) { len = len / 2; for (int i = 0; i < len; i++) {// 分組 for (int j = i + len; j < array.length; j += len) { int end = j - len;// end為有序序列最後一位的位數 int temp = array[j];// temp為要插入的值 while (end >= 0 && temp < array[end]) { array[end + len] = array[end]; end -= len; } array[end + len] = temp; } } } System.out.println("希爾排序:" + Arrays.toString(array)); } public static void main(String[] args) { ShellSort.shellSort(NUMBERS); } }
- 直接選擇排序
/** * 3.直接選擇排序 * 原理:將序列劃分為無序和有序區,尋找無序區中的最小值和無序區的首元素交換,有序區擴大一個,迴圈最終完成全部排序。選擇排序的基本思想是對待排序的記錄序列進行n-1遍的處理,第i遍處理是將L[i..n]中最小者與L[i]交換位置。這樣經過i遍處理之後,前i個記錄的位置已經是正確的了。 * */ public class SelectSort { // 排序原始資料 private static final int[] NUMBERS = {62, 99, 98, 54, 56, 17, 18, 23, 34, 15}; public static void selectSort(int[] array) { int position; for (int i = 0; i < array.length; i++) { int j; position = i; int temp = array[i]; for (j = i + 1; j < array.length; j++) { if (array[j] < temp) { temp = array[j]; position = j; } } array[position] = array[i]; array[i] = temp; } System.out.println("選擇排序:" + Arrays.toString(array)); } public static void main(String[] args) { SelectSort.selectSort(NUMBERS); } }
- 堆排序
/**
* 4.堆排序
* 原理:利用大頂堆或小頂堆思想,首先建立堆,然後將堆首與堆尾交換,堆尾之後為有序區。
* 要點:建堆、交換、調整堆
* 兩個步驟:1.建堆
* 2.對頂與堆的最後一個元素交換位置
*
*/
public class HeapSort {
// 排序原始資料
private static final int[] NUMBERS = {62, 99, 98, 54, 56, 17, 18, 23, 34, 15};
public static void sort(int[] arr) {
// 1.構建大頂堆
for (int i = arr.length / 2 - 1; i >= 0; i--) {
//從第一個非葉子結點從下至上,從右至左調整結構
adjustHeap(arr,i,arr.length);
}
// 2.調整堆結構 + 交換堆頂元素與末尾元素
for(int j = arr.length - 1; j > 0; j--){
swap(arr, 0, j);// 將堆頂元素與末尾元素進行交換
adjustHeap(arr, 0, j);// 重新對堆進行調整
}
}
/**
* 調整大頂堆(僅是調整過程,建立在大頂堆已構建的基礎上)
* @param arr
* @param i
* @param length
*/
public static void adjustHeap(int []arr, int i, int length) {
int temp = arr[i];// 先取出當前元素i
for (int k = i * 2 + 1; k < length; k = k * 2 + 1) {// 從i結點的左子結點開始,也就是2i+1處開始
if(k + 1 < length && arr[k] < arr[k + 1]) {// 如果左子結點小於右子結點,k指向右子結點
k++;
}
if (arr[k] > temp) {// 如果子節點大於父節點,將子節點值賦給父節點(不用進行交換)
arr[i] = arr[k];
i = k;
} else {
break;
}
}
arr[i] = temp;// 將temp值放到最終的位置
}
/**
* 交換元素
* @param arr
* @param a
* @param b
*/
public static void swap(int []arr, int a , int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static void main(String []args) {
sort(NUMBERS);
System.out.println("堆排序:" + Arrays.toString(NUMBERS));
}
}
- 氣泡排序
/**
* 5.氣泡排序(穩定)
* 原理:將序列劃分為無序和有序區,不斷通過交換較大元素至無序區尾完成排序。
* 要點:設計交換判斷條件,提前結束以排好序的序列迴圈。
*
*/
public class BubbleSort {
// 排序原始資料
private static final int[] NUMBERS = {62, 99, 98, 54, 56, 17, 18, 23, 34, 15};
public static void bubbleSort(int[] array) {
int temp;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j+1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("氣泡排序:" + Arrays.toString(array));
}
public static void main(String[] args) {
BubbleSort.bubbleSort(NUMBERS);
}
}
- 快速排序
/**
* 6.快速排序
* 原理:不斷尋找一個序列的中點,然後對中點左右的序列遞迴的進行排序,直至全部序列排序完成,使用了分治的思想。
* 要點:遞迴、分治
*
*/
public class QuickSort {
// 排序原始資料
private static final int[] NUMBERS = {62, 99, 98, 54, 56, 17, 18, 23, 34, 15};
// 分割
public static int partition(int[] a, int p, int r) {
int x = a[r];
int i = p;
int temp;
for (int j = p; j <= r; j++) {
if (a[j] < x) {
temp = a[j];
a[j] = a[i];
a[i] = temp;
i++;
}
}
temp = a[r];
a[r] = a[i];
a[i] = temp;
return i;
}
public static void quickSort(int[] a, int p, int r) {
if (p < r) {
int q = partition(a, p, r);
quickSort(a, p, q - 1);
quickSort(a, q + 1, r);
}
}
public static void main(String[] args) {
quickSort(NUMBERS, 0, 9);
System.out.println("快速排序:" + Arrays.toString(NUMBERS));
}
}
- 歸併排序
/**
* 7.歸併排序(穩定)
* 原理:將原序列劃分為有序的兩個序列,然後利用歸併演算法進行合併,合併之後即為有序序列。
* 要點:歸併、分治
*/
public class MergeSort {
// 排序原始資料
private static final int[] NUMBERS = {62, 99, 98, 54, 56, 17, 18, 23, 34, 15};
// 歸併
public static void merge(int[] a, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int i = low;// 左指標
int j = mid + 1;// 右指標
int k = 0;
// 把較小的數先移到新陣列中
while (i <= mid && j <= high) {
if (a[i] < a[j]) {
temp[k++] = a[i++];
} else {
temp[k++] = a[j++];
}
}
// 把左邊剩餘的數移入陣列
while (i <= mid) {
temp[k++] = a[i++];
}
// 把右邊邊剩餘的數移入陣列
while (j <= high) {
temp[k++] = a[j++];
}
// 把新陣列中的數覆蓋a陣列
for (int m = 0; m < temp.length; m++) {
a[m + low] = temp[m];
}
}
public static void mergeSort(int[] a, int low, int high) {
int mid = (low + high) / 2;
if (low < high) {
// 左邊
mergeSort(a, low, mid);
// 右邊
mergeSort(a, mid + 1, high);
// 左右歸併
merge(a, low, mid, high);
// System.out.println(Arrays.toString(a));
}
}
public static void main(String[] args) {
mergeSort(NUMBERS, 0, NUMBERS.length - 1);
System.out.println("歸併排序:" + Arrays.toString(NUMBERS));
}
}
- 基數排序
/**
* 8.基數排序(穩定)
* 原理:將數字按位數劃分出n個關鍵字,每次針對一個關鍵字進行排序,然後針對排序後的序列進行下一個關鍵字的排序,迴圈至所有關鍵字都使用過則排序完成。
* 要點:對關鍵字的選取,元素分配收集。
*
*/
public class RadixSort {
// 排序原始資料
private static final int[] NUMBERS = {62, 99, 98, 54, 56, 17, 18, 23, 34, 15};
private static void radixSort(int[] array, int d) {
int n = 1;// 代表位數對應的數:1,10,100...
int k = 0;// 儲存每一位排序後的結果用於下一位的排序輸入
int length = array.length;
// 排序桶用於儲存每次排序後的結果,這一位上排序結果相同的數字放在同一個桶裡
int[][] bucket = new int[10][length];
int[] order = new int[length];// 用於儲存每個桶裡有多少個數字
while (n < d) {
for (int num : array) {//將陣列array裡的每個數字放在相應的桶裡
int digit = (num / n) % 10;
bucket[digit][order[digit]] = num;
order[digit]++;
}
// 將前一個迴圈生成的桶裡的資料覆蓋到原陣列中用於儲存這一位的排序結果
for (int i = 0; i < length; i++) {
if (order[i] != 0) {// 這個桶裡有資料,從上到下遍歷這個桶並將資料儲存到原陣列中
for (int j = 0; j < order[i]; j++) {
array[k] = bucket[i][j];
k++;
}
}
order[i] = 0;// 將桶裡計數器置0,用於下一次位排序
}
n *= 10;
k = 0;// 將k置0,用於下一輪儲存位排序結果
}
}
public static void main(String[] args) {
radixSort(NUMBERS, 100);
System.out.println("基數排序:" + Arrays.toString(NUMBERS));
}
}