曬太陽

          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 曬太陽 閱讀(110) 評論(0)  編輯  收藏


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


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

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 会昌县| 齐河县| 平定县| 基隆市| 靖宇县| 彩票| 京山县| 新源县| 五莲县| 两当县| 上栗县| 襄城县| 泸州市| 新兴县| 宁明县| 忻州市| 报价| 惠东县| 桐柏县| 长乐市| 珲春市| 杭锦后旗| 交口县| 麻江县| 南宁市| 邯郸市| 凯里市| 乐亭县| 上林县| 隆化县| 惠东县| 达孜县| 浦城县| 荔波县| 灌南县| 宜春市| 沾化县| 北海市| 绵阳市| 双牌县| 安乡县|