• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
目标驱动者
目标........努力........生活.........
博客园    首页    新随笔    联系   管理    订阅  订阅

C#的快速排序算法

快递排序算法

 The pseudocode of QuickSort is following:

     QUICKSORT(A, p, r )
          if p < r
          then q ←PARTITION(A, p, r )
                 QUICKSORT(A, p, q − 1)
                 QUICKSORT(A, q + 1, r )

=================================
          PARTITION(A, p, r )
               x ← A[r ]
               i ← p − 1
               for j ← p to r − 1
                    do if A[ j ] ≤ x
                         then i ← i + 1
                              exchange A[i ] ↔ A[ j ]
               exchange A[i + 1] ↔ A[r ]
               return i + 1

 

Code
 1namespace QuickSort
 2{
 3    public class Class1
 4    {
 5        
 6        
 7        static void Main()
 8        {
 9            int[] arraySort = new int[]{12,25,41,5,7,85,45,16,89,54,11,23,46 };
10            QuickSorter quickSort = new QuickSorter();
11            int[] sorted=quickSort.QuickSort(arraySort,0,arraySort.Length-1);
12            for (int i = 0; i < sorted.Length - 1;i++ )
13            {
14                Console.Write(sorted[i]+"  ");
15            }

16            Console.Read();
17        }

18    }

19
20    public class QuickSorter
21    {
22        public int[] QuickSort(int[] array, int p, int r)
23        {
24            if (p < r)
25            {
26                int q = Partition(array, p, r);
27                QuickSort(array, p, q - 1);
28                QuickSort(array, q + 1, r);
29            }

30            return array;
31        }

32
33        public int Partition(int[] array, int p, int r)
34        {
35            int x = array[r];
36            int i = p - 1;
37            for (int j = p; j <= r - 1; j++)
38            {
39                if (array[j] <= x)
40                {
41                    i += 1;
42                    int temp;
43                    temp = array[i];
44                    array[i] = array[j];
45                    array[j] = temp;
46                }

47
48            }

49            int temp2;
50            temp2 = array[i + 1];
51            array[i + 1] = array[r];
52            array[r] = temp2;
53            return i+1;
54        }

55
56    }

57}
posted @ 2008-09-04 17:37  IsionWu  阅读(364)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3