曬太陽

          java 快速排序

           1 
           2 public class QuickSort {
           3     public static int partition(int[] arr, int low, int high){
           4         int pivot = arr[low];
           5         while(low<high){
           6             while(low<high && arr[high]>=pivot){
           7                 high--;
           8             }
           9             if(low<high){
          10                 arr[low] = arr[high];
          11                 low++;
          12             }
          13             
          14             while(low<high && arr[low]<=pivot){
          15                 low++;
          16             }
          17             if(low<high){
          18                 arr[high] = arr[low];
          19                 high--;
          20             }
          21         }
          22         arr[low] = pivot;
          23         return low;
          24     }
          25     
          26     public static void sort(int[] arr, int low, int high){
          27         int pivot;
          28         if(low<high){
          29             pivot = partition(arr,low,high);
          30             sort(arr,low,pivot-1);
          31             sort(arr,pivot+1,high);
          32         }
          33     }
          34     
          35     public static void quickSort(int[] arr){
          36         sort(arr,0,arr.length-1);
          37     }
          38     
          39     /*public static void main(String[] args) {
          40         int[] arr = {46,34,2,99,6,23,20,8};
          41         quickSort(arr);
          42         StringBuilder sb = new StringBuilder();
          43         for(int i=0;i<arr.length;i++){
          44             sb.append(arr[i]+",");
          45         }
          46         sb.deleteCharAt(sb.length()-1);
          47         System.out.println(sb);
          48     }*/    
          49 }
          50 

          posted on 2012-11-01 12:06 曬太陽 閱讀(109) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2012年11月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 蓬莱市| 商水县| 商南县| 舟山市| 元江| 分宜县| 毕节市| 泸水县| 伊吾县| 东阿县| 竹山县| 眉山市| 惠水县| 盐津县| 波密县| 从江县| 武冈市| 闸北区| 积石山| 金堂县| 朝阳县| 正镶白旗| 萨嘎县| 祁东县| 成安县| 南丰县| SHOW| 江油市| 新巴尔虎左旗| 珲春市| 乐山市| 陇南市| 临海市| 珠海市| 长武县| 墨玉县| 米脂县| 曲松县| 越西县| 惠东县| 永春县|