posts - 12, comments - 3, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          快速排序

          Posted on 2010-01-17 16:14 創意恒動力 閱讀(348) 評論(0)  編輯  收藏
          快速排序介紹:
          快速排序(Quicksort)是對冒泡法的一種改進。由C. A. R. Hoare在1962年提出。

          基本思想是:
          通過一趟排序將要排序的數據分割成獨立的兩部分,其中一部分的所有數據都比另外一部分的所有數據都要小,然后再按此方法對這兩部分數據分別進行快速排序,整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。

           1 package my.sort;
           2 
           3 public class QuickSort {
           4 
           5     static int a[] = { 101113164325456 };
           6 
           7     public static void sort(int L, int R) {
           8         int i = L - 1;
           9         int j = R;
          10         int tmp;
          11         if (L < R) {
          12             for (; i < R;) {
          13                 while (a[++i] > a[R]) {// 從i開始,從前往后掃描,如果發現大于a[R](數組最后一個值),與之對換
          14                     while (j > 0) {
          15                         if (j <= i) {// 如果i == j結束跳出循環
          16                             break;
          17                         }
          18                         if (a[--j] < a[R]) {// 從J開始,從后往前掃描,如果發現小于a[i],與之對換
          19                             tmp = a[j];
          20                             a[j] = a[i];
          21                             a[i] = tmp;
          22                         }
          23                         
          24                     }
          25                     
          26                     tmp = a[i];
          27                     a[i] = a[R];
          28                     a[R] = tmp;
          29                     
          30                     for(int b : a) {
          31                         System.out.print(b + " ");//打印沒一趟排序結果
          32                     }
          33                     System.out.println();
          34                     
          35                     //把數組分成兩段排序
          36                     sort(L, i-1);//基準數前面的數據進行排列
          37                     sort(i, R);//基準數后面的數據排列
          38                 }
          39             }
          40         }
          41 
          42     }
          43 
          44     public static void main(String[] args) {
          45         System.out.println("排序前:");
          46         for (int b : a) {
          47             System.out.print(b + " ");
          48         }
          49         System.out.println("\n排序過程:");
          50         sort(0, a.length - 1);
          51         System.out.println("排序后:");
          52         for (int b : a) {
          53             System.out.print(b + " ");
          54         }
          55         System.out.println();
          56     }
          57 }
          58 


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


          網站導航:
           
          主站蜘蛛池模板: 奉新县| 卢湾区| 靖远县| 渝中区| 南通市| 宣汉县| 怀远县| 乐平市| 江北区| 天峻县| 池州市| 梅州市| 弥渡县| 淳化县| 墨竹工卡县| 长寿区| 汶上县| 安徽省| 旌德县| 盐山县| 元氏县| 天峻县| 聂荣县| 怀宁县| 阿拉善盟| 顺义区| 郁南县| 根河市| 石门县| 望都县| 道孚县| 武义县| 清涧县| 新巴尔虎左旗| 东乡县| 潼关县| 台东市| 鄂伦春自治旗| 阳西县| 洪江市| 工布江达县|