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

          2010年1月17日

          快速排序介紹:
          快速排序(Quicksort)是對冒泡法的一種改進。由C. A. R. Hoare在1962年提出。

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

           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開始,從前往后掃描,如果發(fā)現(xiàn)大于a[R](數(shù)組最后一個值),與之對換
          14                     while (j > 0) {
          15                         if (j <= i) {// 如果i == j結(jié)束跳出循環(huán)
          16                             break;
          17                         }
          18                         if (a[--j] < a[R]) {// 從J開始,從后往前掃描,如果發(fā)現(xiàn)小于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 + " ");//打印沒一趟排序結(jié)果
          32                     }
          33                     System.out.println();
          34                     
          35                     //把數(shù)組分成兩段排序
          36                     sort(L, i-1);//基準數(shù)前面的數(shù)據(jù)進行排列
          37                     sort(i, R);//基準數(shù)后面的數(shù)據(jù)排列
          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 

          posted @ 2010-01-17 16:14 創(chuàng)意恒動力 閱讀(348) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 增城市| 松原市| 乌兰浩特市| 宜兰县| 南宫市| 新兴县| 台南县| 鄱阳县| 嘉黎县| 高阳县| 阜南县| 邵东县| 三穗县| 桃园县| 吉隆县| 渭南市| 江孜县| 新泰市| 贵溪市| 河津市| 宁河县| 延边| 城口县| 沭阳县| 青冈县| 改则县| 新田县| 郑州市| 玛多县| 永顺县| 宽甸| 十堰市| 周至县| 临猗县| 宜兴市| 胶南市| 班戈县| 凯里市| 大埔区| 温泉县| 锡林郭勒盟|