`

【转】java排序

    博客分类:
  • java
 
阅读更多

Java代码  收藏代码
  1. package sort;  
  2.   
  3. import java.util.Random;  
  4.   
  5. /** 
  6.  * 排序测试类 
  7.  *  
  8.  * 排序算法的分类如下: 1.插入排序(直接插入排序、折半插入排序、希尔排序); 2.交换排序(冒泡泡排序、快速排序); 
  9.  * 3.选择排序(直接选择排序、堆排序); 4.归并排序; 5.基数排序。 
  10.  *  
  11.  * 关于排序方法的选择: (1)若n较小(如n≤50),可采用直接插入或直接选择排序。 
  12.  * 当记录规模较小时,直接插入排序较好;否则因为直接选择移动的记录数少于直接插人,应选直接选择排序为宜。 
  13.  * (2)若文件初始状态基本有序(指正序),则应选用直接插人、冒泡或随机的快速排序为宜; 
  14.  * (3)若n较大,则应采用时间复杂度为O(nlgn)的排序方法:快速排序、堆排序或归并排序。 
  15.  *  
  16.  */  
  17. /** 
  18.  * @description JAVA排序汇总 
  19.  */  
  20. public class SortTest {  
  21.   
  22.     // //////==============================产生随机数==============================///////////////////  
  23.     /** 
  24.      * @description 生成随机数 
  25.      * @date Nov 19, 2009 
  26.      * @author HDS 
  27.      * @return int[] 
  28.      */  
  29.     public static  int[] createArray() {  
  30.         Random random = new Random();  
  31.         int[] array = new int[10];  
  32.         for (int i = 0; i < 10; i++) {  
  33.             array[i] = random.nextInt(100) - random.nextInt(100);// 生成两个随机数相减,保证生成的数中有负数  
  34.         }  
  35.         System.out.println("==========原始序列==========");  
  36.         printArray(array);  
  37.         return array;  
  38.     }  
  39.   
  40.     /** 
  41.      * @description 打印出随机数 
  42.      * @date Nov 19, 2009 
  43.      * @author HDS 
  44.      * @param data 
  45.      */  
  46.     public static void printArray(int[] data) {  
  47.         for (int i : data) {  
  48.             System.out.print(i + " ");  
  49.         }  
  50.         System.out.println();  
  51.     }  
  52.   
  53.     /** 
  54.      * @description 交换相邻两个数 
  55.      * @date Nov 19, 2009 
  56.      * @author HDS 
  57.      * @param data 
  58.      * @param x 
  59.      * @param y 
  60.      */  
  61.     public static void swap(int[] data, int x, int y) {  
  62.         int temp = data[x];  
  63.         data[x] = data[y];  
  64.         data[y] = temp;  
  65.     }  
  66.   
  67.     /** 
  68.      * 冒泡排序----交换排序的一种 
  69.      * 方法:相邻两元素进行比较,如有需要则进行交换,每完成一次循环就将最大元素排在最后(如从小到大排序),下一次循环是将其他的数进行类似操作。 
  70.      * 性能:比较次数O(n^2),n^2/2;交换次数O(n^2),n^2/4 
  71.      *  
  72.      * @param data 
  73.      *            要排序的数组 
  74.      * @param sortType 
  75.      *            排序类型 
  76.      * @return 
  77.      */  
  78.     public void bubbleSort(int[] data, String sortType) {  
  79.         if (sortType.equals("asc")) { // 正排序,从小排到大  
  80.             // 比较的轮数  
  81.             for (int i = 1; i < data.length; i++) { // 数组有多长,轮数就有多长  
  82.                 // 将相邻两个数进行比较,较大的数往后冒泡  
  83.                 for (int j = 0; j < data.length - i; j++) {// 每一轮下来会将比较的次数减少  
  84.                     if (data[j] > data[j + 1]) {  
  85.                         // 交换相邻两个数  
  86.                         swap(data, j, j + 1);  
  87.                     }  
  88.                 }  
  89.             }  
  90.         } else if (sortType.equals("desc")) { // 倒排序,从大排到小  
  91.             // 比较的轮数  
  92.             for (int i = 1; i < data.length; i++) {  
  93.                 // 将相邻两个数进行比较,较大的数往后冒泡  
  94.                 for (int j = 0; j < data.length - i; j++) {  
  95.                     if (data[j] < data[j + 1]) {  
  96.                         // 交换相邻两个数  
  97.                         swap(data, j, j + 1);  
  98.                     }  
  99.                 }  
  100.             }  
  101.         } else {  
  102.             System.out.println("您输入的排序类型错误!");  
  103.         }  
  104.         printArray(data);// 输出冒泡排序后的数组值  
  105.     }  
  106.   
  107.     /** 
  108.      * 直接选择排序法----选择排序的一种 方法:每一趟从待排序的数据元素中选出最小(或最大)的一个元素, 
  109.      * 顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。 性能:比较次数O(n^2),n^2/2 交换次数O(n),n 
  110.      * 交换次数比冒泡排序少多了,由于交换所需CPU时间比比较所需的CUP时间多,所以选择排序比冒泡排序快。 
  111.      * 但是N比较大时,比较所需的CPU时间占主要地位,所以这时的性能和冒泡排序差不太多,但毫无疑问肯定要快些。 
  112.      *  
  113.      * @param data 
  114.      *            要排序的数组 
  115.      * @param sortType 
  116.      *            排序类型 
  117.      * @return 
  118.      */  
  119.     public void selectSort(int[] data, String sortType) {  
  120.         if (sortType.endsWith("asc")) {// 正排序,从小排到大  
  121.             int index;  
  122.             for (int i = 1; i < data.length; i++) {  
  123.                 index = 0;  
  124.                 for (int j = 1; j <= data.length - i; j++) {  
  125.                     if (data[j] > data[index]) {  
  126.                         index = j;  
  127.                     }  
  128.                 }  
  129.                 // 交换在位置data.length-i和index(最大值)两个数  
  130.                 swap(data, data.length - i, index);  
  131.             }  
  132.         } else if (sortType.equals("desc")) { // 倒排序,从大排到小  
  133.             int index;  
  134.             for (int i = 1; i < data.length; i++) {  
  135.                 index = 0;  
  136.                 for (int j = 1; j <= data.length - i; j++) {  
  137.                     if (data[j] < data[index]) {  
  138.                         index = j;  
  139.                     }  
  140.                 }  
  141.                 // 交换在位置data.length-i和index(最大值)两个数  
  142.                 swap(data, data.length - i, index);  
  143.             }  
  144.         } else {  
  145.             System.out.println("您输入的排序类型错误!");  
  146.         }  
  147.         printArray(data);// 输出直接选择排序后的数组值  
  148.     }  
  149.   
  150.     /** 
  151.      * 插入排序 方法:将一个记录插入到已排好序的有序表(有可能是空表)中,从而得到一个新的记录数增1的有序表。 性能:比较次数O(n^2),n^2/4 
  152.      * 复制次数O(n),n^2/4 比较次数是前两者的一般,而复制所需的CPU时间较交换少,所以性能上比冒泡排序提高一倍多,而比选择排序也要快。 
  153.      *  
  154.      * @param data 
  155.      *            要排序的数组 
  156.      * @param sortType 
  157.      *            排序类型 
  158.      */  
  159.     public void insertSort(int[] data, String sortType) {  
  160.         if (sortType.equals("asc")) { // 正排序,从小排到大  
  161.             // 比较的轮数  
  162.             for (int i = 1; i < data.length; i++) {  
  163.                 // 保证前i+1个数排好序  
  164.                 for (int j = 0; j < i; j++) {  
  165.                     if (data[j] > data[i]) {  
  166.                         // 交换在位置j和i两个数  
  167.                         swap(data, i, j);  
  168.                     }  
  169.                 }  
  170.             }  
  171.         } else if (sortType.equals("desc")) { // 倒排序,从大排到小  
  172.             // 比较的轮数  
  173.             for (int i = 1; i < data.length; i++) {  
  174.                 // 保证前i+1个数排好序  
  175.                 for (int j = 0; j < i; j++) {  
  176.                     if (data[j] < data[i]) {  
  177.                         // 交换在位置j和i两个数  
  178.                         swap(data, i, j);  
  179.                     }  
  180.                 }  
  181.             }  
  182.         } else {  
  183.             System.out.println("您输入的排序类型错误!");  
  184.         }  
  185.         printArray(data);// 输出插入排序后的数组值  
  186.     }  
  187.   
  188.     /** 
  189.      * 反转数组的方法 
  190.      *  
  191.      * @param data 
  192.      *            源数组 
  193.      */  
  194.     public void reverse(int[] data) {  
  195.         int length = data.length;  
  196.         int temp = 0;// 临时变量  
  197.         for (int i = 0; i < length / 2; i++) {  
  198.             temp = data[i];  
  199.             data[i] = data[length - 1 - i];  
  200.             data[length - 1 - i] = temp;  
  201.         }  
  202.         printArray(data);// 输出到转后数组的值  
  203.     }  
  204.   
  205.     /** 
  206.      * 快速排序 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists)。 步骤为: 
  207.      * 1. 从数列中挑出一个元素,称为 "基准"(pivot), 2. 
  208.      * 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分割之后,该基准是它的最后位置。这个称为分割(partition)操作。 
  209.      * 3. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。 
  210.      * 递回的最底部情形,是数列的大小是零或一,也就是永远都已经被排序好了。虽然一直递回下去,但是这个算法总会结束,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。 
  211.      *  
  212.      * @param data 
  213.      *            待排序的数组 
  214.      * @param low 
  215.      * @param high 
  216.      * @see SortTest#qsort(int[], int, int) 
  217.      * @see SortTest#qsort_desc(int[], int, int) 
  218.      */  
  219.     public void quickSort(int[] data, String sortType) {  
  220.         if (sortType.equals("asc")) { // 正排序,从小排到大  
  221.             qsort_asc(data, 0, data.length - 1);  
  222.         } else if (sortType.equals("desc")) { // 倒排序,从大排到小  
  223.             qsort_desc(data, 0, data.length - 1);  
  224.         } else {  
  225.             System.out.println("您输入的排序类型错误!");  
  226.         }  
  227.     }  
  228.   
  229.     /** 
  230.      * 快速排序的具体实现,排正序 
  231.      *  
  232.      * @param data 
  233.      * @param low 
  234.      * @param high 
  235.      */  
  236.     private void qsort_asc(int data[], int low, int high) {  
  237.         int i, j, x;  
  238.         if (low < high) { // 这个条件用来结束递归  
  239.             i = low;  
  240.             j = high;  
  241.             x = data[i];  
  242.             while (i < j) {  
  243.                 while (i < j && data[j] > x) {  
  244.                     j--; // 从右向左找第一个小于x的数  
  245.                 }  
  246.                 if (i < j) {  
  247.                     data[i] = data[j];  
  248.                     i++;  
  249.                 }  
  250.                 while (i < j && data[i] < x) {  
  251.                     i++; // 从左向右找第一个大于x的数  
  252.                 }  
  253.                 if (i < j) {  
  254.                     data[j] = data[i];  
  255.                     j--;  
  256.                 }  
  257.             }  
  258.             data[i] = x;  
  259.             qsort_asc(data, low, i - 1);  
  260.             qsort_asc(data, i + 1, high);  
  261.         }  
  262.     }  
  263.   
  264.     /** 
  265.      * 快速排序的具体实现,排倒序 
  266.      *  
  267.      * @param data 
  268.      * @param low 
  269.      * @param high 
  270.      */  
  271.     private void qsort_desc(int data[], int low, int high) {  
  272.         int i, j, x;  
  273.         if (low < high) { // 这个条件用来结束递归  
  274.             i = low;  
  275.             j = high;  
  276.             x = data[i];  
  277.             while (i < j) {  
  278.                 while (i < j && data[j] < x) {  
  279.                     j--; // 从右向左找第一个小于x的数  
  280.                 }  
  281.                 if (i < j) {  
  282.                     data[i] = data[j];  
  283.                     i++;  
  284.                 }  
  285.                 while (i < j && data[i] > x) {  
  286.                     i++; // 从左向右找第一个大于x的数  
  287.                 }  
  288.                 if (i < j) {  
  289.                     data[j] = data[i];  
  290.                     j--;  
  291.                 }  
  292.             }  
  293.             data[i] = x;  
  294.             qsort_desc(data, low, i - 1);  
  295.             qsort_desc(data, i + 1, high);  
  296.         }  
  297.     }  
  298.   
  299.     /** 
  300.      * 二分查找特定整数在整型数组中的位置(递归) 查找线性表必须是有序列表 
  301.      *  
  302.      * @paramdataset 
  303.      * @paramdata 
  304.      * @parambeginIndex 
  305.      * @paramendIndex 
  306.      * @returnindex 
  307.      */  
  308.     public int binarySearch(int[] dataset, int data, int beginIndex,  
  309.             int endIndex) {  
  310.         int midIndex = (beginIndex + endIndex) >>> 1// 相当于mid = (low + high)  
  311.                                                         // / 2,但是效率会高些  
  312.         if (data < dataset[beginIndex] || data > dataset[endIndex]  
  313.                 || beginIndex > endIndex)  
  314.             return -1;  
  315.         if (data < dataset[midIndex]) {  
  316.             return binarySearch(dataset, data, beginIndex, midIndex - 1);  
  317.         } else if (data > dataset[midIndex]) {  
  318.             return binarySearch(dataset, data, midIndex + 1, endIndex);  
  319.         } else {  
  320.             return midIndex;  
  321.         }  
  322.     }  
  323.   
  324.     /** 
  325.      * 二分查找特定整数在整型数组中的位置(非递归) 查找线性表必须是有序列表 
  326.      *  
  327.      * @paramdataset 
  328.      * @paramdata 
  329.      * @returnindex 
  330.      */  
  331.     public int binarySearch(int[] dataset, int data) {  
  332.         int beginIndex = 0;  
  333.         int endIndex = dataset.length - 1;  
  334.         int midIndex = -1;  
  335.         if (data < dataset[beginIndex] || data > dataset[endIndex]  
  336.                 || beginIndex > endIndex)  
  337.             return -1;  
  338.         while (beginIndex <= endIndex) {  
  339.             midIndex = (beginIndex + endIndex) >>> 1// 相当于midIndex =  
  340.                                                         // (beginIndex +  
  341.                                                         // endIndex) / 2,但是效率会高些  
  342.             if (data < dataset[midIndex]) {  
  343.                 endIndex = midIndex - 1;  
  344.             } else if (data > dataset[midIndex]) {  
  345.                 beginIndex = midIndex + 1;  
  346.             } else {  
  347.                 return midIndex;  
  348.             }  
  349.         }  
  350.         return -1;  
  351.     }  
  352.   
  353.     // /////////////////////===================================测试====================//////////////////  
  354.     public static void main(String[] args) {  
  355.         SortTest ST = new SortTest();  
  356.         int[] array = ST.createArray();  
  357.         System.out.println("==========冒泡排序后(正序)==========");  
  358.         ST.bubbleSort(array, "asc");  
  359.         System.out.println("==========冒泡排序后(倒序)==========");  
  360.         ST.bubbleSort(array, "desc");  
  361.   
  362.         array = ST.createArray();  
  363.         System.out.println("==========选择排序后(正序)==========");  
  364.         ST.selectSort(array, "asc");  
  365.         System.out.println("==========选择排序后(倒序)==========");  
  366.         ST.selectSort(array, "desc");  
  367.           
  368.         array = ST.createArray();  
  369.         System.out.println("==========插入排序后(正序)==========");  
  370.         ST.insertSort(array, "asc");  
  371.         System.out.println("==========插入排序后(倒序)==========");  
  372.         ST.insertSort(array, "desc");  
  373.   
  374.         array = ST.createArray();  
  375.         System.out.println("==========快速排序后(正序)==========");  
  376.         ST.quickSort(array, "asc");  
  377.         ST.printArray(array);  
  378.         System.out.println("==========快速排序后(倒序)==========");  
  379.         ST.quickSort(array, "desc");  
  380.         ST.printArray(array);  
  381.         System.out.println("==========数组二分查找==========");  
  382.         System.out.println("您要找的数在第" + ST.binarySearch(array, 74)+ "个位子。(下标从0计算)");  
  383.   
  384.     }  
  385.   
  386. }   

  ================

 

上面是比较全的,下面是我重写时的记录:

 

 

Java代码  收藏代码
  1. package sort;  
  2.   
  3. import static sort.SortTest.*;  
  4.   
  5. public class RmnTest {  
  6.   
  7.     /** 
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) {  
  11.         int[] array = SortTest.createArray();  
  12.           
  13.         bubbleSort(array);  
  14.           
  15.         printArray(array);  
  16.           
  17.         array = SortTest.createArray();  
  18.           
  19.         selectSort(array);  
  20.         printArray(array);  
  21.     }  
  22.   
  23.     /* 
  24.      * 这样的只记下标,更能体现出选择排序的概念 
  25.      */  
  26.     private static void selectSort(int[] array) {  
  27.         for(int i = 0;i<array.length;i++) {  
  28.             int tempIndex = i;  
  29.             for(int j = 0;j<array.length;j++) {  
  30.                 if(array[j] < array[tempIndex]) {  
  31.                     tempIndex = j;  
  32.                 }  
  33.             }  
  34.             if(tempIndex != i)  swap(array,tempIndex,i);  
  35.         }  
  36.     }  
  37.   
  38.     /* 
  39.      * 像下面这样地比较j和j+1的值更能体现出冒泡的意义  
  40.      */  
  41.     private static void bubbleSort(int[] array) {  
  42.         for(int i = 1 ;i< array.length;i++) {  
  43.             for(int j  = 0;j<array.length-1;j++) {  
  44.                 if(array[j] < array[j+1]) {  
  45.                     swap(array,j,j+1);  
  46.                 }  
  47.             }  
  48.         }  
  49.     }  
  50.       
  51.       
  52.   
  53. //  private static void selectSort(int[] array) {  
  54. //      for(int outer = 0;outer < array.length - 1;outer ++) {  
  55. //          int temp= array[outer];  
  56. //          for(int in = outer; in < array.length; in ++) {  
  57. //              if(array[in] < temp ) {  
  58. //                  temp = array[in];  
  59. //                  swap(array,outer,in);  
  60. //              }  
  61. //          }  
  62. //      }  
  63. //  }  
  64. //  
  65. //  private static void bubbleSort(int[] array) {  
  66. //      for(int i = 0;i<array.length  -1 ;i++) {  
  67. //          for(int j = i + 1;j<array.length;j++) {  
  68. //              if(array[i] < array[j]) {  
  69. //                  swap(array,i,j);  
  70. //              }  
  71. //          }  
  72. //      }  
  73. //  }  
  74.   
  75. }  
 

==============================================

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics