快速排序

来源:互联网 发布:影响淘宝关键词 编辑:程序博客网 时间:2024/06/06 14:10


        下面为java实现的快速排序

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 实现快速排序  
  3.      快速排序是目前所有排序中性能较好的一种算法,最好情况和平均情况下时间复杂度均为O(nlogn),最坏的情况下时间复杂度为O(n^2)。快速排序采用递归,用空间换取时间。由于使用了递归,因此需要额外的存储空间。

         快速排序由三个函数构成,分别为QuickSort(int[] arr)、QuickSort(int[] arr,int start,int end)、partition(int[] arr,int start,int end)。

         其中partition函数能够从当前待排序序列中找出一个主元,并使得主元左侧的元素均小于主元,主元右侧的元素均大于主元。

         Partition函数完成对待排序序列的分割后,交给QuickSort(int[] arr,int start,int end)处理,QuickSort分别将被Partition分隔的两个子序列进行快速排序。

         QuickSort(int[] arr)是整个快速排序的入口函数,用户只需传入待排序数组即可。


  4.  * @author zyf
  5.  */  
  6. public class QuickSort {  
  7.       
  8.     /** 
  9.      * 本函数实现一趟快速排序,以数组的第一个元素为主元, 
  10.      * 本函数运行结束后使得主元左侧的元素小于主元,主元右侧的元素大于主元。 
  11.      * @param arr 待排序的数组 
  12.      * @return 返回经一趟排序后主元的下标 
  13.      */  
  14.     private static int partition(int[] arr,int start,int end){  
  15.         //健壮性判断  
  16.         if(arr.length<=0){  
  17.             System.out.println("数组为空!");  
  18.             return -1;  
  19.         }  
  20.         if(start<0 || end<0 || start>end){  
  21.             System.out.println("start、end非法!");  
  22.             return -1;  
  23.         }  
  24.           
  25.         //i指向数组头、j指向数组尾  
  26.         int i=start+1,j=end;  
  27.         //选择数组第一位为主元  
  28.         int key = arr[start];  
  29.         //若i与j未相遇,则执行以下循环  
  30.         while(i<j){  
  31.             //i从左向右扫描,直到当前元素大于主元时停下  
  32.             while(arr[i]<=key && i<end){  
  33.                 i++;  
  34.             }  
  35.             //j从右向左扫描,直到当前元素小于主元时停下  
  36.             while(arr[j]>=key && j>start){  
  37.                 j--;  
  38.             }  
  39.             //i、j停下有两种情况:1.  
  40.             if(i<j)  
  41.                 swap(arr,i,j);  
  42.         }  
  43.         //将主元与j交换  
  44.         swap(arr,start,j);  
  45.         System.out.println("某一趟排序结果:"+printArray(arr));  
  46.         //返回新的主元下标  
  47.         return j;  
  48.     }  
  49.       
  50.       
  51.       
  52.     /** 
  53.      * 快速排序入口函数 
  54.      * @param arr 待排序数组 
  55.      * @return 返回有序的数组 
  56.      */  
  57.     public static void QuickSort(int[] arr){  
  58.         //健壮性判断  
  59.         if(arr==null || arr.length<=0){  
  60.             System.out.println("数组为空!");  
  61.             return;  
  62.         }  
  63.           
  64.         //通过递归进行快速排序  
  65.         QuickSort(arr,0,arr.length-1);  
  66.     }  
  67.       
  68.       
  69.       
  70.     /** 
  71.      * 快速排序的递归函数 
  72.      * @param arr 待排序数组 
  73.      * @param start 数组的起始下标 
  74.      * @param end 数组的结束下标 
  75.      * @return 返回当前有序子序列 
  76.      */  
  77.     private static void QuickSort(int[] arr,int start,int end){  
  78.         if(start<end){  
  79.             //获取主元  
  80.             int key = partition(arr,start,end);  
  81.             //对主元左侧的序列进行快速排序  
  82.             QuickSort(arr,start,key-1);  
  83.             //对主元右侧的序列进行快速排序  
  84.             QuickSort(arr,key+1,end);  
  85.         }  
  86.     }  
  87.       
  88.       
  89.     /** 
  90.      * 实现i与j位置的元素交换 
  91.      * @param arr 数组 
  92.      * @param i 下标 
  93.      * @param j 下标 
  94.      */  
  95.     private static void swap(int[] arr,int i,int j){  
  96.         //健壮性判断  
  97.         if(arr==null || arr.length<=0){  
  98.             System.out.println("数组为空!");  
  99.             return;  
  100.         }  
  101.           
  102.         //定义临时变量temp实现交换  
  103.         int temp = arr[i];  
  104.         arr[i] = arr[j];  
  105.         arr[j] = temp;  
  106.     }  
  107.       
  108.       
  109.       
  110.     /** 
  111.      * 输出数组元素 
  112.      * @param arr 
  113.      * @return 
  114.      */  
  115.     private static String printArray(int[] arr){  
  116.         if(arr==null){  
  117.             System.out.println("数组为空!");  
  118.             return null;  
  119.         }  
  120.           
  121.         StringBuffer sb = new StringBuffer();  
  122.         for(int i=0;i<arr.length;i++){  
  123.             sb.append(arr[i]);  
  124.         }  
  125.           
  126.         return sb.toString();  
  127.     }  
  128. }  
0 0
原创粉丝点击