排序

  1. 冒泡排序
  2. import java.util.Random;   
  3. /**  
  4.  * 依次比較相鄰的兩個數,將小數放在前面,大數放在後面  
  5.  * 冒泡排序,具有穩定性  
  6.  * 時間複雜度爲O(n^2)  
  7.  * 不及堆排序,快速排序O(nlogn,底數爲2)  
  8.  * @author liangge  
  9.  *  
  10.  */  
  11. public class Main {   
  12.     public static void main(String[] args) {   
  13.         Random ran = new Random();   
  14.         int[] sort = new int[10];   
  15.         for(int i = 0 ; i < 10 ; i++){   
  16.             sort[i] = ran.nextInt(50);   
  17.         }   
  18.         System.out.print("排序前的數組爲");   
  19.         for(int i : sort){   
  20.             System.out.print(i+" ");   
  21.         }   
  22.         buddleSort(sort);   
  23.         System.out.println();   
  24.         System.out.print("排序後的數組爲");   
  25.         for(int i : sort){   
  26.             System.out.print(i+" ");   
  27.         }   
  28.     }   
  29.        
  30.     /**  
  31.      * 冒泡排序  
  32.      * @param sort  
  33.      */  
  34.     private static void buddleSort(int[] sort){   
  35.         for(int i=1;i<sort.length;i++){   
  36.             for(int j=0;j<sort.length-i;j++){   
  37.                 if(sort[j]>sort[j+1]){   
  38.                     int temp = sort[j+1];   
  39.                     sort[j+1] = sort[j];   
  40.                     sort[j] = temp;   
  41.                 }   
  42.             }   
  43.         }   
  44.     }   
  45.  

選擇排序

 

import java.util.Random;       /**    * 選擇排序    * 每一趟從待排序的數據元素中選出最小(或最大)的一個元素,    * 順序放在已排好序的數列的最後,直到全部待排序的數據元素排完。     * 選擇排序是不穩定的排序方法。    * @author liangge    *     */   public class Main {        public static void main(String[] args) {            Random ran = new Random();            int[] sort = new int[10];            for (int i = 0; i < 10; i++) {                sort[i] = ran.nextInt(50);            }            System.out.print("排序前的數組爲");            for (int i : sort) {                System.out.print(i + " ");            }            selectSort(sort);            System.out.println();            System.out.print("排序後的數組爲");            for (int i : sort) {                System.out.print(i + " ");            }        }           /**        * 選擇排序        * @param sort        */       private static void selectSort(int[] sort){            for(int i =0;i<sort.length-1;i++){                for(int j = i+1;j<sort.length;j++){                    if(sort[j]<sort[i]){                        int temp = sort[j];                        sort[j] = sort[i];                        sort[i] = temp;                    }                }            }        }    }  

 

 

快速排序

 

 

  1. /**  
  2.  * 快速排序 通過一趟排序將要排序的數據分割成獨立的兩部分, 其中一部分的所有數據都比另外一部分的所有數據都要小,  
  3.  * 然後再按此方法對這兩部分數據分別進行快速排序, 整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。  
  4.  * @author liangge  
  5.  *   
  6.  */  
  7. public class Main {   
  8.     public static void main(String[] args) {   
  9.         int[] sort = { 5431893366126820 };   
  10.         System.out.print("排序前的數組爲:");   
  11.         for (int data : sort) {   
  12.             System.out.print(data + " ");   
  13.         }   
  14.         System.out.println();   
  15.         quickSort(sort, 0, sort.length - 1);   
  16.         System.out.print("排序後的數組爲:");   
  17.         for (int data : sort) {   
  18.             System.out.print(data + " ");   
  19.         }   
  20.     }   
  21.   
  22.     /**  
  23.      * 快速排序  
  24.      * @param sort 要排序的數組  
  25.      * @param start 排序的開始座標  
  26.      * @param end 排序的結束座標  
  27.      */  
  28.     public static void quickSort(int[] sort, int start, int end) {   
  29.         // 設置關鍵數據key爲要排序數組的第一個元素,   
  30.         // 即第一趟排序後,key右邊的數全部比key大,key左邊的數全部比key小   
  31.         int key = sort[start];   
  32.         // 設置數組左邊的索引,往右移動判斷比key大的數   
  33.         int i = start;   
  34.         // 設置數組右邊的索引,往左移動判斷比key小的數   
  35.         int j = end;   
  36.         // 如果左邊索引比右邊索引小,則還有數據沒有排序   
  37.         while (i < j) {   
  38.             while (sort[j] > key && j > start) {   
  39.                 j--;   
  40.             }   
  41.             while (sort[i] < key && i < end) {   
  42.                 i++;   
  43.             }   
  44.             if (i < j) {   
  45.                 int temp = sort[i];   
  46.                 sort[i] = sort[j];   
  47.                 sort[j] = temp;   
  48.             }   
  49.         }   
  50.         // 如果左邊索引比右邊索引要大,說明第一次排序完成,將sort[j]與key對換,   
  51.         // 即保持了key左邊的數比key小,key右邊的數比key大   
  52.         if (i > j) {   
  53.             int temp = sort[j];   
  54.             sort[j] = sort[start];   
  55.             sort[start] = temp;   
  56.         }   
  57.         //遞歸調用   
  58.         if (j > start && j < end) {   
  59.             quickSort(sort, start, j - 1);   
  60.             quickSort(sort, j + 1, end);   
  61.         }   
  62.     }   
  63. }  

 

插入排序

 

  1. /**  
  2.  * 直接插入排序  
  3.  * 將一個數據插入到已經排好序的有序數據中,從而得到一個新的、個數加一的有序數據  
  4.  * 算法適用於少量數據的排序,時間複雜度爲O(n^2)。是穩定的排序方法。  
  5.  */  
  6. import java.util.Random;   
  7.   
  8. public class DirectMain {   
  9.     public static void main(String[] args) {   
  10.         Random ran = new Random();   
  11.         int[] sort = new int[10];   
  12.         for (int i = 0; i < 10; i++) {   
  13.             sort[i] = ran.nextInt(50);   
  14.         }   
  15.         System.out.print("排序前的數組爲");   
  16.         for (int i : sort) {   
  17.             System.out.print(i + " ");   
  18.         }   
  19.         directInsertSort(sort);   
  20.         System.out.println();   
  21.         System.out.print("排序後的數組爲");   
  22.         for (int i : sort) {   
  23.             System.out.print(i + " ");   
  24.         }   
  25.     }   
  26.   
  27.     /**  
  28.      * 直接插入排序  
  29.      *   
  30.      * @param sort  
  31.      */  
  32.     private static void directInsertSort(int[] sort) {   
  33.         for (int i = 1; i < sort.length; i++) {   
  34.             int index = i - 1;   
  35.             int temp = sort[i];   
  36.             while (index >= 0 && sort[index] > temp) {   
  37.                 sort[index + 1] = sort[index];   
  38.                 index--;   
  39.             }   
  40.             sort[index + 1] = temp;   
  41.   
  42.         }   
  43.     }   
  44. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章