Java 的八種排序演算法

Java 的八種排序演算法

 

    這個世界,需要遺忘的太多。

 

背景:工作三年,演算法一問三不知。

一、八種排序演算法

直接插入排序、希爾排序、簡單選擇排序、堆排序、冒泡排序、快速排序、歸併排序和基數排序。

二、演算法使用

1 直接插入排序

使用場景:

如把新的數據插入到已排好的數據列中。

實現思想:

a、將第一個數和第二個數排序,然後構成一個有序序列;

b、將第三個數插入進去,構成一個新的有序序列;

c、對第四個數、第五個數……直到最後一個數,重複第二步。

 程式碼實現:

 1 /**
 2  * @author tjt
 3  * @time 2020-09-02
 4  * Java 排序演算法
 5  */
 6 public class JavaSortAlgorithm {
 7 
 8 
 9     /**
10      * 直接插入排序:
11      * 首先,設定插入次數,即循環次數,for(int i=1;i<length;i++),第1個數的那次不用插入,會有元素間的比較排序
12      * 然後,設定插入數和得到已經排好序列的最後一個數的位數:insertNum和j=i-1
13      * 從最後一個數開始向前循環,如果插入數小於當前數,就將當前數向後移動一位
14      * 將當前數放置到空著的位置,即j+1。
15      *
16      * @param array
17      */
18     private static void justInsertSort(int[] array) {
19         System.out.println("***********直接插入排序*************");
20         int length = array.length;
21         // insertNum 為要插入的數
22         int insertNum;
23         for (int i = 1; i < length; i++) {
24             insertNum = array[i];
25             System.out.println("insertNum: " + insertNum);
26             // 已經排序好的序列元素個數
27             int j = i - 1;
28             System.out.println(Arrays.toString(array));
29             // 序列從後到前循環,將大於insertNum 的數向後移動一格
30             while (j >= 0 && array[j] > insertNum) {
31                 // 元素移動一格
32                 array[j + 1] = array[j];
33                 // j-- 之後繼續於之前的比較,從後往前
34                 j--;
35             }
36             // 將需要插入的數放在要插入的位置。
37             array[j + 1] = insertNum;
38         }
39     }
40 
41 }

View Code~拍一拍

直接插入排序結果驗證:

2 希爾排序

使用場景:

對於直接插入排序問題,數據量巨大時,可以考慮使用希爾排序。

實現思想:

a、將數的個數設為n,取奇數k=n/2,將下標差值為k 的樹分為一組,構成有序序列;

b、再取k=k/2 ,將下標差值為k 的數分為一組,構成有序序列;

c、重複第二步,直到k=1 執行簡單插入排序。

 程式碼實現:

 1     /**
 2      * 希爾排序:
 3      * 首先,確定分的組數,然後對組中元素進行插入排序
 4      * 接下來,將length/2,重複1,2步,直到length=0 為止。
 5      *
 6      * @param array
 7      */
 8     private static void xiErSort(int[] array) {
 9         System.out.println("***********希爾排序*************");
10         int length = array.length;
11         while (length != 0) {
12             length = length / 2;
13             // 分的數組
14             for (int x = 0; x < length; x++) {
15                 // 組中的元素,從第二個數開始
16                 for (int i = x + length; i < array.length; i += length) {
17                     // j為有序序列最後一位的位數
18                     int j = i - length;
19                     // temp 為要插入的元素
20                     int temp = array[i];
21                     // 從後往前遍歷
22                     for (; j >= 0 && temp < array[j]; j -= length) {
23                         // 向後移動length 位
24                         array[j + length] = array[j];
25                     }
26                     array[j + length] = temp;
27                 }
28             }
29         }
30     }

View Code~拍一拍

希爾排序結果驗證:

3 簡單選擇排序

使用場景:

常用於取序列中最大最小的幾個數。(如果每次比較都交換,那麼就是交換排序;如果每次比較完一個循環再交換,就是簡單選擇排序。)

實現思想:

a、遍歷整個序列,將最小的數放在最前面;

b、遍歷剩下的序列,將最小的數放在最前面;

c、重複第二步,直到只剩下一個數。

 程式碼實現:

 1  /**
 2      * 簡單選擇排序:
 3      * 首先確定循環次數,並且記住當前數字和當前位置。
 4      * 將當前位置後面所有的數與當前數字進行對比,小數賦值給key,並記住小數的位置。
 5      * 比對完成後,將最小的值與第一個數的值交換。
 6      * 重複2、3步。
 7      *
 8      * @param array
 9      */
10     private static void simpleSelectSort(int[] array) {
11         System.out.println("***********簡單選擇排序*************");
12         int length = array.length;
13         // 循環次數
14         for (int i = 0; i < length; i++) {
15             int key = array[i];
16             int position = i;
17             // 選出最小的值和位置
18             for (int j = i + 1; j < length; j++) {
19                 if (array[j] < key) {
20                     key = array[j];
21                     position = j;
22                 }
23             }
24             // 交換位置
25             array[position] = array[i];
26             array[i] = key;
27         }
28     }

View Code~小輪胎

簡單選擇排序結果驗證:

4 堆排序

使用場景:

堆排序使用場景與簡單排序相似,其是簡單排序的優化。

實現思想:

a、將序列構建成大頂堆;

b、將根節點與最後一個節點交換,然後斷開最後一個節點;

c、重複第一、二步,直到所有節點斷開。

 程式碼實現:

 1  /**
 2      * 堆排序:
 3      * 將序列構建成大頂堆;
 4      * 將根節點與最後一個節點交換,然後斷開最後一個節點;
 5      * 重複第一、二步,直到所有節點斷開。
 6      *
 7      * @param array
 8      */
 9     public static void heapSort(int[] array) {
10         System.out.println("***********堆排序*************");
11         System.out.println("開始排序:");
12         int arrayLength = array.length;
13         // 循環建堆
14         for (int i = 0; i < arrayLength - 1; i++) {
15             // 建堆
16             buildMaxHeap(array, arrayLength - 1 - i);
17             // 交換堆頂和最後一個元素
18             swap(array, 0, arrayLength - 1 - i);
19             System.out.println(Arrays.toString(array));
20         }
21     }

View Code~小輪胎

堆排序結果驗證:

5 冒泡排序

使用場景:

一般比較少使用冒泡排序,徐工說會哥冒泡排序出去面試就有15K。

實現思想:

a、將序列中所有元素兩兩比較,將最大的放在最後面。

b、將剩餘序列中所有元素兩兩比較,將最大的放在最後面。

c、重複第二步,直到只剩下一個數

 程式碼實現:

 1  /**
 2      * 冒泡排序:
 3      * 設置循環次數。
 4      * 設置開始比較的位數,和結束的位數。
 5      * 兩兩比較,將最小的放到前面去。
 6      * 重複2、3步,直到循環次數完畢。
 7      *
 8      * @param array
 9      */
10     private static void bubbleSort(int[] array) {
11         int length = array.length;
12         int temp;
13         for (int i = 0; i < length; i++) {
14             for (int j = 0; j < length - i - 1; j++) {
15                 if (array[j] > array[j + 1]) {
16                     temp = array[j];
17                     array[j] = array[j + 1];
18                     array[j + 1] = temp;
19                 }
20             }
21         }
22     }

View Code~拍一拍

冒泡排序結果驗證:

6 快速排序

使用場景:

對排序時間要求較高的情況下可以考慮使用快排。

實現思想:

a、選擇第一個數為p,小於p的數放在左邊,大於p的數放在右邊;

b、遞歸的將p左邊和右邊的數都按照第一步進行,直到不能遞歸。

 程式碼實現:

 1 /**
 2      * 快速排序:
 3      * 選擇第一個數為p,小於p的數放在左邊,大於p的數放在右邊;
 4      * 遞歸的將p左邊和右邊的數都按照第一步進行,直到不能遞歸。
 5      *
 6      * @param array
 7      * @param start
 8      * @param end
 9      */
10     private static void quicklySort(int[] array, int start, int end) {
11         System.out.println("***********快速排序*************");
12         if (start < end) {
13             // 選定的基準值(第一個數值作為基準值)
14             int base = array[start];
15             // 記錄臨時中間值
16             int temp;
17             int i = start, j = end;
18             do {
19                 while ((array[i] < base) && (i < end)) {
20                     i++;
21                 }
22                 while ((array[j] > base) && (j > start)) {
23                     j--;
24                 }
25                 if (i <= j) {
26                     temp = array[i];
27                     array[i] = array[j];
28                     array[j] = temp;
29                     i++;
30                     j--;
31                 }
32             } while (i <= j);
33             if (start < j) {
34                 quicklySort(array, start, j);
35             }
36             if (end > i) {
37                 quicklySort(array, i, end);
38             }
39         }
40     }

View Code~拍一拍

快速排序結果驗證:

7 歸併排序

使用場景:

速度僅次於快排,在記憶體少的時候使用、可以進行並行計算的時候使用。

實現思想:

a、選擇相鄰兩個數組成一個有序序列;

b、選擇相鄰的兩個有序序列組成一個有序序列;

c、重複第二步,直到全部組成一個有序序列。

 程式碼實現:

 1  /**
 2      * 歸併排序:
 3      * 選擇相鄰兩個數組成一個有序序列;
 4      * 選擇相鄰的兩個有序序列組成一個有序序列;
 5      * 重複第二步,直到全部組成一個有序序列。
 6      *
 7      * @param arr
 8      * @param l
 9      * @param r
10      */
11     private static void mergeSort(int[] arr, int l, int r) {
12         System.out.println("***********歸併排序*************");
13         if (l < r) {
14             int q = (l + r) / 2;
15             mergeSort(arr, l, q);
16             mergeSort(arr, q + 1, r);
17             merge(arr, l, q, r);
18         }
19     }
20 
21     /**
22      * @param arr 排序數組
23      * @param l   數組最左邊下標
24      * @param q   數組中間位置下標
25      * @param r   數組最右位置下標
26      */
27     private static void merge(int[] arr, int l, int q, int r) {
28         /**
29          * 因為每次切割後左邊下標都是(l,q),右邊數組的下標是(q+1,r)
30          * 所以左邊數組的元素個數就是q - l + 1
31          * 右邊的數組元素個數就是r - q
32          */
33         // 切割後左邊數組的數據長度
34         final int n1 = q - l + 1;
35         // 切割後右邊數組的數據長度
36         final int n2 = r - q;
37         /**創建兩個新數組將切割後的數組分別放進去,長度加1是為了放置無窮大的數據標誌位**/
38         // 加一操作是增加無窮大標誌位
39         final int[] left = new int[n1 + 1];
40         // 加一操作是增加無窮大標誌位
41         final int[] right = new int[n2 + 1];
42         //兩個循環將數據添加至新數組中
43         /**左邊的數組下標是從l到q**/
44         /**遍歷左邊的數組*/
45         for (int i = 0; i < n1; i++) {
46             left[i] = arr[l + i];
47         }
48         for (int i = 0; i < n2; i++) {
49             right[i] = arr[q + 1 + i];
50         }
51 
52         // 將最大的正整數放在兩個新數組的最後一位
53         left[n1] = Integer.MAX_VALUE;
54         right[n2] = Integer.MAX_VALUE;
55 
56         int i = 0, j = 0;
57         // 將小的放在前面
58         for (int k = l; k <= r; k++) {
59             if (left[i] <= right[j]) {
60                 arr[k] = left[i];
61                 i = i + 1;
62             } else {
63                 arr[k] = right[j];
64                 j = j + 1;
65             }
66         }
67     }

View Code~拍一拍

歸併排序結果驗證:

8 基數排序

使用場景:

適用於數目量較大、很長的數進行排序。(排序隊列存在負數除外)

實現思想:

a、將所有的數的個位數取出,按照個位數進行排序,構成一個序列;

b、將新構成的所有的數的十位數取出,按照十位數進行排序,構成一個序列。

 程式碼實現:

 1  /**
 2      * 基數排序:
 3      * 將所有的數的個位數取出,按照個位數進行排序,構成一個序列;
 4      * 將新構成的所有的數的十位數取出,按照十位數進行排序,構成一個序列。
 5      *
 6      * @param array 排序隊列中存在負數除外
 7      */
 8     private static void baseSort(int[] array) {
 9         System.out.println("***********基數排序*************");
10         // 首先確定排序的趟數;
11         int max = array[0];
12         for (int i = 1; i < array.length; i++) {
13             if (array[i] > max) {
14                 max = array[i];
15             }
16         }
17         int time = 0;
18         // 判斷位數;
19         while (max > 0) {
20             max /= 10;
21             time++;
22         }
23         // 建立10個隊列;
24         List<ArrayList> queue = new ArrayList<ArrayList>();
25         for (int i = 0; i < 10; i++) {
26             ArrayList<Integer> queue1 = new ArrayList<Integer>();
27             queue.add(queue1);
28         }
29         // 進行time次分配和收集;
30         for (int i = 0; i < time; i++) {
31             //分配數組元素;
32             for (int j = 0; j < array.length; j++) {
33                 // 得到數字的第time+1位數;
34                 int x = array[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);
35                 ArrayList<Integer> queue2 = queue.get(x);
36                 queue2.add(array[j]);
37                 queue.set(x, queue2);
38             }
39             // 元素計數器;
40             int count = 0;
41             // 收集隊列元素;
42             for (int k = 0; k < 10; k++) {
43                 while (queue.get(k).size() > 0) {
44                     ArrayList<Integer> queue3 = queue.get(k);
45                     array[count] = queue3.get(0);
46                     queue3.remove(0);
47                     count++;
48                 }
49             }
50         }
51     }

View Code~小輪胎

基數排序結果驗證:

八種排序演算法程式碼:

  1 package com.ausclouds.bdbsec.tjt;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Arrays;
  5 import java.util.List;
  6 
  7 /**
  8  * @author tjt
  9  * @time 2020-09-02
 10  * Java 排序演算法
 11  */
 12 public class JavaSortAlgorithm {
 13 
 14 
 15     /**
 16      * 直接插入排序:
 17      * 首先,設定插入次數,即循環次數,for(int i=1;i<length;i++),第1個數的那次不用插入,會有元素間的比較排序
 18      * 然後,設定插入數和得到已經排好序列的最後一個數的位數:insertNum和j=i-1
 19      * 從最後一個數開始向前循環,如果插入數小於當前數,就將當前數向後移動一位
 20      * 將當前數放置到空著的位置,即j+1。
 21      *
 22      * @param array
 23      */
 24     private static void justInsertSort(int[] array) {
 25         System.out.println("***********直接插入排序*************");
 26         int length = array.length;
 27         // insertNum 為要插入的數
 28         int insertNum;
 29         for (int i = 1; i < length; i++) {
 30             insertNum = array[i];
 31             System.out.println("insertNum: " + insertNum);
 32             // 已經排序好的序列元素個數
 33             int j = i - 1;
 34             System.out.println(Arrays.toString(array));
 35             // 序列從後到前循環,將大於insertNum 的數向後移動一格
 36             while (j >= 0 && array[j] > insertNum) {
 37                 // 元素移動一格
 38                 array[j + 1] = array[j];
 39                 // j-- 之後繼續於之前的比較,從後往前
 40                 j--;
 41             }
 42             // 將需要插入的數放在要插入的位置。
 43             array[j + 1] = insertNum;
 44         }
 45     }
 46 
 47     /**
 48      * 希爾排序:
 49      * 首先,確定分的組數,然後對組中元素進行插入排序
 50      * 接下來,將length/2,重複1,2步,直到length=0 為止。
 51      *
 52      * @param array
 53      */
 54     private static void xiErSort(int[] array) {
 55         System.out.println("***********希爾排序*************");
 56         int length = array.length;
 57         while (length != 0) {
 58             length = length / 2;
 59             // 分的數組
 60             for (int x = 0; x < length; x++) {
 61                 // 組中的元素,從第二個數開始
 62                 for (int i = x + length; i < array.length; i += length) {
 63                     // j為有序序列最後一位的位數
 64                     int j = i - length;
 65                     // temp 為要插入的元素
 66                     int temp = array[i];
 67                     // 從後往前遍歷
 68                     for (; j >= 0 && temp < array[j]; j -= length) {
 69                         // 向後移動length 位
 70                         array[j + length] = array[j];
 71                     }
 72                     array[j + length] = temp;
 73                 }
 74             }
 75         }
 76     }
 77 
 78     /**
 79      * 簡單選擇排序:
 80      * 首先確定循環次數,並且記住當前數字和當前位置。
 81      * 將當前位置後面所有的數與當前數字進行對比,小數賦值給key,並記住小數的位置。
 82      * 比對完成後,將最小的值與第一個數的值交換。
 83      * 重複2、3步。
 84      *
 85      * @param array
 86      */
 87     private static void simpleSelectSort(int[] array) {
 88         System.out.println("***********簡單選擇排序*************");
 89         int length = array.length;
 90         // 循環次數
 91         for (int i = 0; i < length; i++) {
 92             int key = array[i];
 93             int position = i;
 94             // 選出最小的值和位置
 95             for (int j = i + 1; j < length; j++) {
 96                 if (array[j] < key) {
 97                     key = array[j];
 98                     position = j;
 99                 }
100             }
101             // 交換位置
102             array[position] = array[i];
103             array[i] = key;
104         }
105     }
106 
107 
108     /**
109      * 堆排序:
110      * 將序列構建成大頂堆;
111      * 將根節點與最後一個節點交換,然後斷開最後一個節點;
112      * 重複第一、二步,直到所有節點斷開。
113      *
114      * @param array
115      */
116     public static void heapSort(int[] array) {
117         System.out.println("***********堆排序*************");
118         System.out.println("開始排序:");
119         int arrayLength = array.length;
120         // 循環建堆
121         for (int i = 0; i < arrayLength - 1; i++) {
122             // 建堆
123             buildMaxHeap(array, arrayLength - 1 - i);
124             // 交換堆頂和最後一個元素
125             swap(array, 0, arrayLength - 1 - i);
126             System.out.println(Arrays.toString(array));
127         }
128     }
129 
130     /**
131      * 交換堆頂和最後一個元素
132      *
133      * @param data
134      * @param i
135      * @param j
136      */
137     private static void swap(int[] data, int i, int j) {
138         int tmp = data[i];
139         data[i] = data[j];
140         data[j] = tmp;
141     }
142 
143     /**
144      * 建堆:對data數組從0到lastIndex建大頂堆
145      *
146      * @param data
147      * @param lastIndex
148      */
149     private static void buildMaxHeap(int[] data, int lastIndex) {
150         // 從lastIndex處節點(最後一個節點)的父節點開始
151         for (int i = (lastIndex - 1) / 2; i >= 0; i--) {
152             // k保存正在判斷的節點
153             int k = i;
154             // 如果當前k節點的子節點存在
155             while (k * 2 + 1 <= lastIndex) {
156                 // k節點的左子節點的索引
157                 int biggerIndex = 2 * k + 1;
158                 //如果biggerIndex小於lastIndex,即biggerIndex+1代表的k節點的右子節點存在
159                 if (biggerIndex < lastIndex) {
160                     // 若果右子節點的值較大
161                     if (data[biggerIndex] < data[biggerIndex + 1]) {
162                         // biggerIndex總是記錄較大子節點的索引
163                         biggerIndex++;
164                     }
165                 }
166                 // 如果k節點的值小於其較大的子節點的值
167                 if (data[k] < data[biggerIndex]) {
168                     // 交換他們
169                     swap(data, k, biggerIndex);
170                     // 將biggerIndex賦予k,開始while循環的下一次循環,重新保證k節點的值大於其左右子節點的值
171                     k = biggerIndex;
172                 } else {
173                     break;
174                 }
175             }
176         }
177     }
178 
179     /**
180      * 冒泡排序:
181      * 設置循環次數。
182      * 設置開始比較的位數,和結束的位數。
183      * 兩兩比較,將最小的放到前面去。
184      * 重複2、3步,直到循環次數完畢。
185      *
186      * @param array
187      */
188     private static void bubbleSort(int[] array) {
189         int length = array.length;
190         int temp;
191         for (int i = 0; i < length; i++) {
192             for (int j = 0; j < length - i - 1; j++) {
193                 if (array[j] > array[j + 1]) {
194                     temp = array[j];
195                     array[j] = array[j + 1];
196                     array[j + 1] = temp;
197                 }
198             }
199         }
200     }
201 
202     /**
203      * 快速排序:
204      * 選擇第一個數為p,小於p的數放在左邊,大於p的數放在右邊;
205      * 遞歸的將p左邊和右邊的數都按照第一步進行,直到不能遞歸。
206      *
207      * @param array
208      * @param start
209      * @param end
210      */
211     private static void quicklySort(int[] array, int start, int end) {
212         System.out.println("***********快速排序*************");
213         if (start < end) {
214             // 選定的基準值(第一個數值作為基準值)
215             int base = array[start];
216             // 記錄臨時中間值
217             int temp;
218             int i = start, j = end;
219             do {
220                 while ((array[i] < base) && (i < end)) {
221                     i++;
222                 }
223                 while ((array[j] > base) && (j > start)) {
224                     j--;
225                 }
226                 if (i <= j) {
227                     temp = array[i];
228                     array[i] = array[j];
229                     array[j] = temp;
230                     i++;
231                     j--;
232                 }
233             } while (i <= j);
234             if (start < j) {
235                 quicklySort(array, start, j);
236             }
237             if (end > i) {
238                 quicklySort(array, i, end);
239             }
240         }
241     }
242 
243     /**
244      * 歸併排序:
245      * 選擇相鄰兩個數組成一個有序序列;
246      * 選擇相鄰的兩個有序序列組成一個有序序列;
247      * 重複第二步,直到全部組成一個有序序列。
248      *
249      * @param arr
250      * @param l
251      * @param r
252      */
253     private static void mergeSort(int[] arr, int l, int r) {
254         System.out.println("***********歸併排序*************");
255         if (l < r) {
256             int q = (l + r) / 2;
257             mergeSort(arr, l, q);
258             mergeSort(arr, q + 1, r);
259             merge(arr, l, q, r);
260         }
261     }
262 
263     /**
264      * @param arr 排序數組
265      * @param l   數組最左邊下標
266      * @param q   數組中間位置下標
267      * @param r   數組最右位置下標
268      */
269     private static void merge(int[] arr, int l, int q, int r) {
270         /**
271          * 因為每次切割後左邊下標都是(l,q),右邊數組的下標是(q+1,r)
272          * 所以左邊數組的元素個數就是q - l + 1
273          * 右邊的數組元素個數就是r - q
274          */
275         // 切割後左邊數組的數據長度
276         final int n1 = q - l + 1;
277         // 切割後右邊數組的數據長度
278         final int n2 = r - q;
279         /**創建兩個新數組將切割後的數組分別放進去,長度加1是為了放置無窮大的數據標誌位**/
280         // 加一操作是增加無窮大標誌位
281         final int[] left = new int[n1 + 1];
282         // 加一操作是增加無窮大標誌位
283         final int[] right = new int[n2 + 1];
284         //兩個循環將數據添加至新數組中
285         /**左邊的數組下標是從l到q**/
286         /**遍歷左邊的數組*/
287         for (int i = 0; i < n1; i++) {
288             left[i] = arr[l + i];
289         }
290         for (int i = 0; i < n2; i++) {
291             right[i] = arr[q + 1 + i];
292         }
293 
294         // 將最大的正整數放在兩個新數組的最後一位
295         left[n1] = Integer.MAX_VALUE;
296         right[n2] = Integer.MAX_VALUE;
297 
298         int i = 0, j = 0;
299         // 將小的放在前面
300         for (int k = l; k <= r; k++) {
301             if (left[i] <= right[j]) {
302                 arr[k] = left[i];
303                 i = i + 1;
304             } else {
305                 arr[k] = right[j];
306                 j = j + 1;
307             }
308         }
309     }
310 
311     /**
312      * 基數排序:
313      * 將所有的數的個位數取出,按照個位數進行排序,構成一個序列;
314      * 將新構成的所有的數的十位數取出,按照十位數進行排序,構成一個序列。
315      *
316      * @param array 排序隊列中存在負數除外
317      */
318     private static void baseSort(int[] array) {
319         System.out.println("***********基數排序*************");
320         // 首先確定排序的趟數;
321         int max = array[0];
322         for (int i = 1; i < array.length; i++) {
323             if (array[i] > max) {
324                 max = array[i];
325             }
326         }
327         int time = 0;
328         // 判斷位數;
329         while (max > 0) {
330             max /= 10;
331             time++;
332         }
333         // 建立10個隊列;
334         List<ArrayList> queue = new ArrayList<ArrayList>();
335         for (int i = 0; i < 10; i++) {
336             ArrayList<Integer> queue1 = new ArrayList<Integer>();
337             queue.add(queue1);
338         }
339         // 進行time次分配和收集;
340         for (int i = 0; i < time; i++) {
341             //分配數組元素;
342             for (int j = 0; j < array.length; j++) {
343                 // 得到數字的第time+1位數;
344                 int x = array[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);
345                 ArrayList<Integer> queue2 = queue.get(x);
346                 queue2.add(array[j]);
347                 queue.set(x, queue2);
348             }
349             // 元素計數器;
350             int count = 0;
351             // 收集隊列元素;
352             for (int k = 0; k < 10; k++) {
353                 while (queue.get(k).size() > 0) {
354                     ArrayList<Integer> queue3 = queue.get(k);
355                     array[count] = queue3.get(0);
356                     queue3.remove(0);
357                     count++;
358                 }
359             }
360         }
361     }
362 
363     /**
364      * 測試排序演算法
365      *
366      * @param args
367      */
368     public static void main(String[] args) {
369         int[] array = new int[]{32, 43, 0, 1314, 23, 4, 12, 5, 520};
370         System.out.println("array's origin sort: " + Arrays.toString(array));
371         //justInsertSort(array);
372         //simpleSelectSort(array);
373         //xiErSort(array);
374         //heapSort(array);
375         //bubbleSort(array);
376         //quicklySort(array, 0, array.length - 1);
377         //mergeSort(array, 0, array.length - 1);
378         baseSort(array);
379         System.out.println("array's sort after use algorithm: " + Arrays.toString(array));
380         int[] array2 = new int[]{32, 43, 0, 1314, 23, -4, 12, 5, 520};
381         baseSort(array2);
382 
383     }
384 
385 
386 }

View Code~主君

 

 

這個世界

      需要遺忘的太多