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);
            }
        }

 

posted @ 2021-03-08 15:00  FredGrit  阅读(69)  评论(0编辑  收藏  举报