C# quick sort

 1 static void QuickSortDemo()
 2         {
 3             int[] arr = new int[] { 2, 5, -4, 11, 0, 18, 22, 67, 51, 6 };
 4 
 5             Console.WriteLine("Original array : ");
 6             foreach (var item in arr)
 7             {
 8                 Console.Write(" " + item);
 9             }
10             Console.WriteLine();
11 
12             QuickSort(arr, 0, arr.Length - 1);
13 
14             Console.WriteLine();
15             Console.WriteLine("Sorted array : ");
16 
17             foreach (var item in arr)
18             {
19                 Console.Write(" " + item);
20             }
21         }
22 
23         static int Partition(int[] arr,int left,int right)
24         {
25             int key = arr[left];
26             while(true)
27             {
28                 while(arr[left]<key)
29                 {
30                     left++;
31                 }
32                 while(arr[right]>key)
33                 {
34                     right--;
35                 }
36                 if(left<right)
37                 {
38                     if(arr[left]==arr[right])
39                     {
40                         return right;
41                     }
42                     int temp = arr[left];
43                     arr[left] = arr[right];
44                     arr[right] = temp;
45                 }
46                 else
47                 {
48                     return right;
49                 }
50             }
51         }
52 
53         static void QuickSort(int[] arr,int left,int right)
54         {
55             if(left<right)
56             {
57                 int key = Partition(arr, left, right);
58                 if(key>1)
59                 {
60                     QuickSort(arr, left, key - 1);
61                 }
62                 if(key+1<right)
63                 {
64                     QuickSort(arr, key + 1, right);
65                 }
66             }
67         }

 

static int Partition(int[] arr,int low,int high)
        {
            int i = low;
            int j = high;
            int pivot = arr[low];
            while(i<j)
            {
                while(i<j && arr[j]>=pivot)
                {
                    j--;
                }
                if(i<j)
                {
                    arr[i++] = arr[j];
                }
                while(i<j && arr[i]<=pivot)
                {
                    i++;
                }
                if(i<j)
                {
                    arr[j--] = arr[i];
                }
            }
            arr[i] = pivot;
            return i;
        }

        static void QuickSort(int[] arr,int low,int high)
        {
            int pivot;
            if(low<high)
            {
                pivot = Partition(arr, low, high);
                QuickSort(arr, low, pivot - 1);
                QuickSort(arr, pivot + 1, high);
            }
        }

 

原文链接: https://www.cnblogs.com/Fred1987/p/14499716.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C# quick sort

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401867

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:28
下一篇 2023年4月19日 上午9:28

相关推荐