Sorting algorithms - quick sort (high-speed sorting) code reading

This note only for my study, review and think with. (Reference Network Code)

Predetermined arr [] = {49, 38, 65, 97, 76, 13, 27}

Algorithm steps:

1, selection point, a predetermined point = index (typically selecting a first number of array)

2, set left, right (left array represents the leftmost i.e. arr [0], right array represents the rightmost i.e. arr [arr.Length - 1], left and right are provided to compare the size and backward traversal point forward)

3, arr [left] and index are compared, if arr [left] is greater than the index into the back or left ++ been backward looking until you find the value of arr [right] is greater than the index and index are compared, if arr [right] is less than the index then to the front or right-- looking straight ahead until you find the value is less than the index of (the reason for this provision is dependent on the array is sorted in descending or ascending order taking, now ascending order of presentation)

4, the set point index into the current intermediate array

5. Repeat 2, steps 3 and 4 until the left == right

Code Example:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 排序__高速排序
 8 {
 9     class Program
10     {
11         public static int[] s = { 49, 38, 65, 97, 76, 13, 27 };
12         static void Main(string[] args)
13         {
14             FastSort(s, 0, s.Length - 1);
15             Console.ReadKey();
16         }
17        
18         private static int SortUnit(int []arr,int left,int right)
19         {
20             int i = left, j = right;
21             int index = arr[left];
22             while(left<right)
23             {
24                 while (left < right && arr[right] > index) right--;
25                 arr[left] = arr[right];
26                 while (left < right && arr[left] < index) left++;
27                 arr[right] = arr[left];
28                 
29             }
30             arr[left] = index;
31             foreach (var item in arr)
32             {
33                 Console.Write("{0}       ", item);
34             }
35             Console.Write("\n");
36             return left;
37         }
38 
39         public static void FastSort(int []s,int left,int right)
40         {
41             if (left >= right) return;
42             int index = SortUnit(s, left, right);
43             FastSort(s, left, index - 1);
44             FastSort(s, index + 1, right);
45         }
46     }
47 }

Complete fragment sorting:

private static int SortUnit(int []arr,int left,int right)
        {
            int i = left, j = right;
            int index = arr[left];
            while(left<right)           //  @1
            {
                while (left < right && arr[right] > index) right--;
                arr[left] = arr[right];
                while (left < right && arr[left] < index) left++;
                arr[right] = arr[left];
                
            }
            arr[left] = index;                           // @2
            foreach (var item in arr)
            {
                Console.Write("{0}       ", item);
            }
            Console.Write("\n");
            return left;            //@4
        }

        public static void FastSort(int []s,int left,int right)   //@5
        {
            if (left >= right) return;
            int index = SortUnit(s, left, right);
            FastSort(s, left, index - 1);
            FastSort(s, index + 1, right);
        }

 

Code Interpretation:

@ 1: the cycle once the array is, the sort mode (ascending say here) is to first find if hit from the back to the front and the index is less than if this number does not exist, until the left and right traverse the same stop, from front to back is relatively the same ( here explain why the comparison is to start to move forward rather than back-to-back to start comparing and select the point index of a relationship, because this is the first example of a selection of the array an element as the base index, so when in comparison exchanges, index will have to save rather than being overwritten, if the first comparison of front to back, then select the starting point must be the last element, so arr [right ] = arr [left] when assigned, it will not overwrite the right] previous value arr [, as it has been saved )

@ 2: Comparative and after completion of substitution, the selected value as an intermediate point of the array, and because the left = right to left it is returned directly to the cutting array (i.e. recursive quicksort is accomplished by a divide and conquer sorted, so the former requires an array into two, the left half of the array repeated sorting step, the right half of the array sorting step is repeated, until the final elements of each array is cut only when the end of a sort)

@ 5: recursive termination condition is left = right i.e. the length of the array is an array of the original cutting is completed i.e., index indicates that the primary array of the first sort, to obtain a better value @ recursion step is repeated 4

 

PS: This algorithm is relatively unstable, is said to have a method of optimization, the process can be such that the probability of the worst case drop occurs, refers to the worst case time complexity of O (n- 2 cases), and then research free

Guess you like

Origin www.cnblogs.com/CHANGKTITI/p/11284032.html