常見的八大排序算法,他們之間關係如下:
代碼實現
冒泡排序:
冒泡排序思路比較簡單:
1、將序列當中的左右元素,依次比較,保證右邊的元素始終大於左邊的元素;
( 第一輪結束後,序列最後一個元素一定是當前序列的最大值;)
2、對序列當中剩下的n-1個元素再次執行步驟1。
3、對於長度爲n的序列,一共需要執行n-1輪比較
(利用while循環可以減少執行次數)
實現:
public static int[] bubbleSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length-1; j++) {
if (arr[i] < arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
快速排序:
1、從序列當中選擇一個基準數(pivot)
在這裏選擇序列當中第一個數最爲基準數
2、將序列當中的所有數依次遍歷,比基準數大的位於其右側,比基準數小的位於其左側
3、重複步驟1.2,直到所有子集當中只有一個元素爲止.
public class Quick {
/**
* 對數組內元素進行排序
*
* @param a
*/
public static void sort(Comparable[] a) {
int lo = 0;
int hi = a.length - 1;
sort(a, lo, hi);
}
/**
* 對數組a中從索引lo到hi之間的元素進行排序
*
* @param a
* @param lo
* @param hi
*/
public static void sort(Comparable[] a, int lo, int hi) {
//安全校驗
if (hi <= lo) {
return;
}
//對索引ol到hi的的元素進行分組
//獲取中間節點
int partition = partition(a, lo, hi);
//對左邊的進行排序
sort(a, lo, partition - 1);
//對右邊進行排序
sort(a, partition + 1, hi);
}
/**
* 對數組a中,從索引lo到hi之將的元素進行分組,並返回分組界限對應的索引
*
* @param a
* @param lo
* @param hi
* @return
*/
public static int partition(Comparable[] a, int lo, int hi) {
//分界值
Comparable key = a[lo];
//定義兩個值,分別指向切分元素最小和最大的索引的下一個位置
int left = lo;
int right = hi + 1;
//切分
while (true) {
//先從右往左掃描,直到比分界值小的元素
while (less(key, a[--right])) {
if (right == lo) break;
}
//從左往右掃描,直到找到比分界值大的元素
while (less(a[++left], key)) {
if (left == hi) break;
}
if (left >= right) break;
else exch(a, left, right);
}
exch(a, lo, right);
return right;
}
/**
* 判斷v是否小於w
*
* @param v
* @param w
* @return
*/
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
/**
* 交換a數組中兩個索引對應的值
*
* @param a
* @param i
* @param j
*/
private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
}
堆排序:
堆的概念
堆:本質是一種數組對象。特別重要的一點性質:任意的葉子節點小於(或大於)它所有的父節點。對此,又分爲大頂堆和小頂堆,大頂堆要求節點的元素都要大於其孩子,小頂堆要求節點元素都小於其左右孩子,兩者對左右孩子的大小關係不做任何要求。
利用堆排序,就是基於大頂堆或者小頂堆的一種排序方法。下面,我們通過大頂堆來實現。
基本思想:
堆排序可以按照以下步驟來完成:
1、首先將序列構建稱爲大頂堆;
(這樣滿足了大頂堆那條性質:位於根節點的元素一定是當前序列的最大值)
2、構建大頂堆
取出當前大頂堆的根節點,將其與序列末尾元素進行交換;
(此時:序列末尾的元素爲已排序的最大值;由於交換了元素,當前位於根節點的堆並不一定滿足大頂堆的性質)
對交換後的n-1個序列元素進行調整,使其滿足大頂堆的性質;
3、重複2.3步驟,直至堆中只有1個元素爲止
/**
* 堆排序
*
* @param arr
*/
private static void headSort(int[] arr) {
//把無序的數組轉換成有序的堆
//1、從最後一個非葉子節點開始遍歷,
//2、以這個非葉子節點爲父節點,將其轉換成大頂堆
int temp = 0;
for (int i = arr.length / 2 - 1; i >= 0; i--) {
head(arr, i, arr.length);
}
//依次取出堆頂元素,放在數組最後面,直到只有一個元素
for (int i = arr.length - 1; i > 0; i--) {
temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
head(arr,0,i);
}
}
private static void head(int[] arr, int i, int length) {
// i * 2 + 1 ---> 以i爲父節點的左子葉節點
int temp = arr[i];
for (int k = i * 2 + 1; k < length; k = 2 * k + 1) {
if (k + 1 < length && arr[k] < arr[k + 1]) {
k++;
}
if (temp < arr[k]) {//與父節點進行比較
arr[i] = arr[k];
i = k;//交換位置以後,打破了之前拍的續,所以如果滿足條件,就繼續遍歷
} else {
break;
}
}
arr[i] = temp;
}
基數排序:
基數排序:通過序列中各個元素的值,對排序的N個元素進行若干趟的“分配”與“收集”來實現排序。
分配:我們將L[i]中的元素取出,首先確定其個位上的數字,根據該數字分配到與之序號相同的桶中
收集:當序列中所有的元素都分配到對應的桶中,再按照順序依次將桶中的元素收集形成新的一個待排序列L[ ]
對新形成的序列L[]重複執行分配和收集元素中的十位、百位…直到分配完該序列中的最高位,則排序結束
/**
* 基數排序
*
* @param arr
*/
public static void radixSort(int[] arr) {
//造出十個桶
int[][] bucket = new int[10][arr.length];
//記錄每個桶中存放了多殺數據
int[] buckerCounts = new int[10];
//獲取數組中最大元素,計算出需要循環遍歷的次數
int max = arr[0];
for (int i = 1; i < arr.length; i++) if (arr[i] > max) max = arr[i];
//計算出最大的數有多少位
String maxString = "" + max;
for (int m = 0, n = 1; m < maxString.length(); m++, n *= 10) {
//把數據放入桶中
for (int i = 0; i < arr.length; i++) {
int element = arr[i] / n % 10;
bucket[element][buckerCounts[element]++] = arr[i];
}
int index = 0;//原數組記數
// int j = 0;//
for (int i = 0; i < buckerCounts.length; i++) {
if (buckerCounts[i] != 0) {
for (int j = 0; j < buckerCounts[i]; j++) {
arr[index++] = bucket[i][j];
}
}
buckerCounts[i] = 0;
}
}
}
歸併排序:
1、歸併排序是建立在歸併操作上的一種有效的排序算法,該算法是採用分治法的一個典型的應用。它的基本操作是:將已有的子序列合併,達到完全有序的序列;即先使每個子序列有序,再使子序列段間有序。
2、歸併排序其實要做兩件事:
- 分解----將序列每次折半拆分 合併
- 將劃分後的序列段兩兩排序合併
因此,歸併排序實際上就是兩個操作,拆分+合併
/**
* 歸併排序
*
* @param arr //原數組長度
* @param left //每個分組最左邊的元素
* @param right //最右邊的元素
* @param temp //備用數組
*/
public static void mergeSort(int[] arr, int left, int right, int[] temp) {
if (left < right) {
int mid = (right + left) / 2;
//對左邊進行分組
mergeSort(arr, left, mid, temp);
//對右邊元素分組
mergeSort(arr, mid + 1, right, temp);
//分組後合併
merge(arr, left, right, mid, temp);
}
}
//歸併合併
public static void merge(int[] arr, int left, int right, int mid, int[] temp) {
int i = left;
int j = mid + 1;//用兩個指針來比較兩個分組中的元素大小
int index = 0;//表示temp數組中的指針
while (i <= mid && j <= right) {
if (arr[i] < arr[j]) temp[index++] = arr[i++];
else temp[index++] = arr[j++];
}
//如果比較完後分組中號有多餘的元素
while (i <= mid) temp[index++] = arr[i++];
while (j <= right) temp[index++] = arr[j++];
//對數組驚醒拷貝
index = 0;
for (int k = left; k <= right; k++) arr[k] = temp[index++];
}
希爾排序:
思想:將待排序數組按照步長gap進行分組,然後將每組的元素利用直接插入排序的方法進行排序;每次將gap折半減小,循環上述操作;當 gap=1時,利用直接插入,完成排序。同樣的:從上面的描述中我們可以發現:希爾排序的總體實現應該由三個循環完成:
- 第一層循環:將gap依次折半,對序列進行分組,直到gap=1
- 第二、三層循環:也即直接插入排序所需要的兩次循環。具體描述見上。
/**
* 希爾排序
*
* @param arr
*/
public static void shellSort(int[] arr) {
int index = 0;
int indexVal = 0;
//分組
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
//對分組遍歷
for (int i = gap; i < arr.length; i++) {
//用插入的思想對每個分組排序
index = i;
indexVal = arr[i];
while (index - gap >= 0 && indexVal < arr[index - gap]) {
arr[index] = arr[index - gap];
index -= gap;
}
arr[index] = indexVal;
}
}
}
選擇排序:
簡單選擇排序的基本思想:比較+交換。
- 從待排序序列中,找到關鍵字最小的元素;
- 如果最小元素不是待排序序列的第一個元素,將其和第一個元素互換;
- 從餘下的 N - 1個元素中,找出關鍵字最小的元素,重複(1)、(2)步,直到排序結束。 因此我們可以發現,簡單選擇排序也是通過兩層循環實現。第一層循環:依次遍歷序列當中的每一個元素 。第二層循環:將遍歷得到的當前元素依次與餘下的元素進行比較,符合最小元素的條件,則交換。
/**
* 選擇排序
* 對數組遍歷,找出最小的元素,與數組前面的元素交換位置
*
* @param arr
*/
public static void selectSort(int[] arr) {
int index = 0;//記錄最小值的索引
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
int min = arr[i];
index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < min) {
min = arr[j];
index = j;
}
}
temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
插入排序:
/**
* 插入排序
*
* @param arr
*/
public static void insertSort(int[] arr) {
int index = 0;//表示有序表的元素的索引
int temp = 0;//表示當前從無序表中取出的元素
for (int i = 1; i < arr.length; i++) {
temp = arr[i];
while (index >= 0 && temp < arr[index]) {
arr[index + 1] = arr[index];
index--;
}
arr[index + 1] = temp;
index = i;
}
}
}
寫完代碼後,比較排序的速度:
8w:
插入排序:789ms
選擇排序:2925ms
希爾排序:25ms
歸併排序:24ms
基數排序:43ms
堆排序:18ms
快速排序:71ms
8000w:
快速排序:45671ms
希爾排序:28601ms
歸併排序:11797ms(數據量大的時候比堆排序快)
基數排序:OutOfMemoryError: Java heap space(可見基數排序佔內存)
堆排序:28390ms