1 public class ThreeTypesOfBaseSort { 2 // ========================== 三種基本排序的效率對比 ============================ 3 public static void main(String[] args) { 4 ThreeTypesOfBaseSort sort = new ThreeTypesOfBaseSort(); 5 6 // 測試百萬級別的數組排序,看三種基本排序的的效率差別: 7 int number = 500000; 8 int[] array = new int[number]; 9 int[] array1 = new int[number]; 10 int[] array2 = new int[number]; 11 for(int i = 0; i < array.length; i++) { 12 int temp = new Random().nextInt(number); 13 array[i] = temp; 14 array1[i] = temp; 15 array2[i] = temp; 16 } 17 System.out.println("數組准備完畢~"); 18 19 long start1 = System.currentTimeMillis(); 20 sort.bubbleSort(array); 21 long end1 = System.currentTimeMillis(); 22 System.out.println("bubbleSort 用時:" + (end1 - start1));//測試結果:當元素個數為5萬時:4157。50萬:430255。100萬:1644079 23 24 long start2 = System.currentTimeMillis(); 25 sort.selectionSort(array1); 26 long end2 = System.currentTimeMillis(); 27 System.out.println("selectSort 用時:" + (end2 - start2));//5萬:727。50萬:74253。100萬:281276 ==》選擇排序比冒泡快了5.7倍。 28 29 long start3 = System.currentTimeMillis(); 30 sort.insertionSort(array2); 31 long end3 = System.currentTimeMillis(); 32 System.out.println("insertionSort 用時:" + (end3 - start3));//5萬:827。50萬:84644。 ==》插入排序比選擇排序稍慢一點。 33 } 34 35 // ========================== BubbleSort ============================ 36 /** 37 * 冒泡排序:相鄰的兩個數進行比較,如果是從小到大排序,則將較大的那個數放后,每次都要交換。 38 39 * 冒泡排序的優化思路: 40 * ① 判空; 41 * ② 元素的個數很特殊時:個數為0、1時; 42 * ③ 數組已經是有序的,或者是數組里所有元素都相同: 43 */ 44 public void bubbleSort(int[] target){ 45 if(target == null){ 46 return; 47 } 48 boolean tag = true;//如果數組是有序的,則不需要再進行交換。 49 for(int i = 0; i < target.length -1 && tag; i++){//外層循環:控制比較的組數:(元素個數-1)次。 50 tag = false; 51 for(int j = 0; j < target.length - i - 1; j++){//內層循環:控制比較的元素:每組比較都從第一個元素開始比較,把每次比較時的max移到后面去,每組的比較次數=target.length-1-i。 52 if(target[j] > target[j + 1]){ 53 int temp = target[j]; 54 target[j] = target[j + 1]; 55 target[j + 1] = temp; 56 tag = true; 57 } 58 } 59 } 60 } 61 62 // ======================= SelectionSort ============================ 63 /** 64 * 選擇排序: 65 * 第一趟從n個元素的數據序列中選出關鍵字最小/大的元素並放在最前位置, 66 * 下一趟從n-1個元素中選出最小/大的元素並放在未排好序元素的最前位置。以此類推,經過n-1趟完成排序。 67 */ 68 public void selectionSort(int[] target){ 69 if(target == null){ 70 return; 71 } 72 for(int i = 0; i < target.length - 1; i++){ 73 int tempIndex = i; 74 for(int j = i + 1; j < target.length; j++){ 75 if(target[tempIndex] > target[j]){ 76 tempIndex = j; 77 } 78 } 79 int temp = target[tempIndex]; 80 target[tempIndex] = target[i]; 81 target[i] = temp; 82 } 83 } 84 85 // ===================== insertSort ==================================== 86 /** 87 * 插入排序:將一個數據插入到已經排好序的有序數據中(一般默認第一個元素是有序的,比較從第二個元素開始), 88 * 從而得到一個新的、個數加一的有序數據,算法適用於少量數據的排序。 89 */ 90 public void insertionSort(int[] target){ 91 if(target == null){ 92 return; 93 } 94 //外層循環控制比較的組數; 95 for(int i = 0; i < target.length - 1; i++){ 96 //內層循環負責找出該組比較中,在已排好序數組之后的第一個無序的元素,並通過比較將這個無序元素插入到有序數組中。 97 for(int j = i + 1; j > 0; j--){ 98 if(target[j - 1] > target[j]){ 99 int temp = target[j]; 100 target[j] = target[j - 1]; 101 target[j - 1] = temp; 102 }else{ 103 break; 104 } 105 } 106 } 107 } 108 }
測試結果:
時間單位:毫秒
1. 冒泡排序:5萬個元素的排序:4157。 50萬:430255。 100萬:1644079
2. 選擇排序:5萬:727。 50萬:74253。 100萬:281276
3. 插入排序:5萬:827。 50萬:84644。
4. 快速排序:5萬:11。 50萬:66。 100萬:136。
總結:
選擇排序 > 插入排序 > 冒泡排序;
選擇排序比插入排序快了1.1倍,比冒泡排序快了越5.7倍。