The fast sorting algorithm is one of the most neutral of sorting algorithms, and also the algorithm used in many built-in sorting types. The algorithm is in the. NET interview and written tests are often examined, the following will detail the algorithm ideas and code examples for fast sorting.
The knowledge points involved are:
- Implementing a fast sorting algorithm using C #
Analyze the problem:
The fast sorting algorithm is one of the most well-known internal sorting algorithms, in fact the realization of the idea is very simple, and in general the performance is high. Here is the basic idea of the algorithm:
- Suppose that the sequence to be sorted is L[M...N], where each element in l[m...midlle-1] is less than L[midlle], and each element in L[MIDLLE+1...N] is greater than L[midlle].
- Recursive call quick sort algorithm, l[m...midlle-1] and L[MIDLLE+1...N] are sorted separately.
- Because it is in-situ sort, the ordered sequence is formed naturally after the recursion ends.
The following code gives an example of an implementation of a quick sort:
1 usingSystem;2 3 namespaceGuying.Demo.ConsoleApp4 {5 classQuickSort6 {7 Static voidMain (string[] args)8 {9 int[] data =New int[] {1,1,4,3,6,7,4,5,0,0};//creating a test arrayTenRun (data,0, data. Length-1);//to quickly sort One for(inti =0; I < data. Length; i++) AConsole.Write ("{0},", Data[i]); - Console.read (); - } the - /// <summary> - ///Fast Sorting algorithm - /// </summary> + /// <param name= "Data" >Sort Arrays</param> - /// <param name= "Low" >Sort Lower Limit</param> + /// <param name= "High" >Sort Upper Limit</param> A Static voidRun (int[] Data,intLowintHigh ) at { - /** - * Simply set the middle value and use it as a quick-sort split point - * Note here is a simple algorithm - * If you want to optimize the algorithm, you can take a random approach to get the split point - * */ in intMiddle = data[(low + high)/2]; - to inti = low, j = high;//set the move and superscript + - //until two sequences are split the Do * { $ //Scan middle Value left elementPanax Notoginseng while(Data[i] < Middle && i < high) i++; - //Scan Middle Value right element the while(Data[j] > middle && J > Low) j--; + //found a pair of exchangeable values A if(I <=j) the { + //Exchange - inttemp =Data[i]; $Data[i] =Data[j]; $DATA[J] =temp; -i++; -j--; the } -} while(I <=j);Wuyi //a recursive comparison of the small sequence of split point elements for quick sorting the if(J >Low ) Run (data, Low, j); - //a recursive comparison of the large sequence of split point elements to quickly sort Wu if(I <High ) Run (data, I, high); - } About } $}
Compiling and executing the above code gives you the following results:
Description
The fast sorting algorithm runs at a worst-case time of O (N2) with an average run time of O (NLGN). Fast-sorting objects are read into memory, so enter internal ordering internally.
The quick sort determines the position of the element based on the comparison keyword, so it belongs to the comparison sort. At the same time, fast sorting has in-situ sorting and instability, which means that no additional sort space is required for fast sorting, but it is not guaranteed that equal element positions are not exchanged.
Answer:
Fast sorting is one of the most efficient internal sorting algorithms, and its basic idea is to divide the sorted object into two columns of sub-sequences, and one of the subsequence values is greater than the value of another subsequence, and further recursively sorts all the subsequence.
《. NET programmer to interview the real problem "learning notes
Interview Guide-please implement a fast sorting algorithm