排序

来源:互联网 发布:javascript bind() 编辑:程序博客网 时间:2024/05/01 10:00
  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.  

选择排序

 

  1. import java.util.Random;   
  2.   
  3. /**  
  4.  * 选择排序  
  5.  * 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,  
  6.  * 顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。   
  7.  * 选择排序是不稳定的排序方法。  
  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.         selectSort(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 selectSort(int[] sort){   
  35.         for(int i =0;i<sort.length-1;i++){   
  36.             for(int j = i+1;j<sort.length;j++){   
  37.                 if(sort[j]<sort[i]){   
  38.                     int temp = sort[j];   
  39.                     sort[j] = sort[i];   
  40.                     sort[i] = temp;   
  41.                 }   
  42.             }   
  43.         }   
  44.     }   
  45. }  
  46.  

 

快速排序

 

 

  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. }  

原创粉丝点击