Reprint: http://www.cnblogs.com/mingmingruyuedlut/archive/2011/08/17/2143062.html
Think of the fast sort, so I realized the fast sorting algorithm in C #: The basic idea of fast sorting: divide and conquer, that is, decomposition, solution, combination. Decomposition: R[low in unordered areas. High] as a datum (usually the first record is selected, and is labeled as keyvalue, with the subscript keyvalueposition) as the datum divided into two smaller sub-intervals r[low,keyvalueposition-1] and r[ keyvalueposition+ 1, high], and all records of the left sub-interval are less than or equal to the Datum record, all records of the right sub-interval are greater than or equal to the Datum record, and the Datum records do not need to participate in subsequent sorting. The key to partitioning is to ask for the location of the benchmark record keyvalueposition. Solve: Quick sort by recursive call to left and right sub-interval r[low. KeyValuePosition-1] and R[keyvalueposition+1..high] Quick sort combinations: when the two recursive calls in the solve step end, their left and right two sub-bands are ordered. For a quick sort, the combination step does not have to do anything and can be seen as an empty operation. The specific process: set the sequence to R[low,high], from which the first is the baseline, set to KeyValue, and then set two pointers I and J, respectively point to the beginning and end of sequence R[low,high] on the position: 1), will I gradually increase, Until a keyword greater than keyvalue is found, 2), the J is gradually reduced until a keyword less than or equal to KeyValue is found, 3), if the number of elements of i<j, that is, r[i,j] is greater than 1, then the interchange r[i] and r[j]; 4), place the Datum record keyvalue in the appropriate position, i.e. where I and J point at the same time (or 1 at the same point), then this position is the new keyvalueposition. Note: Quick sort is an unstable sort, that is, the relative position is indeterminate after the same keyword is sorted. Here is the code for my C # implementation
Using system;using system.collections.generic;using system.linq;using system.text;namespace QuickSort{class QuickSort {static void Main (string[] args) {//declares data for corresponding test int[] MyArray = new int []{45, 36, 18, 53, 72, 30, 48, 93, 15, 36}; MyArray = new int[] {3, 4, 5, 1}; MyArray = new int[] {3, 4, 2, 1}; int lowindex = 0; The starting position of the array (starting at 0) int highindex = myarray.length-1; The end position of the array//Quick sort quicksortfunction (MyArray, Lowindex, Highindex); Array for (int i = 0; i < myarray.length; i++) {Console.WriteLine (myarray[i) After the output is exhausted ]. ToString ()); }}//Quick sort (destination array, starting position of array, ending position of array) private static void Quicksortfunction (int[] array, int low, int high {try {int keyvalueposition; Record key values for subscript//When passing the target array contains twoWhen the element is on, a recursive call is made. (That is, when the destination array is passed with only one element, this sort ends) if (Low < high) {keyvalueposition = Keyv Aluepositionfunction (array, low, high); Gets the key value of the subscript (the core of the Fast Row) Quicksortfunction (array, low, keyValuePosition-1); Recursive invocation, the quick row divides out the left interval quicksortfunction (array, keyvalueposition + 1, high); Recursive call, fast row divided right interval}} catch (Exception ex) {}}//Quick Sort Core part : Determines the position of the key value in the array, which divides the array into two left and right intervals, and the key values are free from the outside. (Returns the key value should be subscript in the array) private static int keyvaluepositionfunction (int[] array, int low, int high) {int Leftindex = low; Record the starting position of the target array (subsequent dynamic left subscript) int rightindex = high; Records the end position of the target array (the right subscript for subsequent dynamics) int keyValue = Array[low]; The first element of the array is the key value of int temp; When (the left dynamic subscript = = Right dynamic subscript) jumps out of the loop while (Leftindex < Rightindex) {while (Leftindex < ; Rightindex && Array[leftiNdex] <= KeyValue)///The dynamic subscript on the left is gradually increased until a subscript greater than KeyValue is found {leftindex++; } while (Leftindex < Rightindex && Array[rightindex] > KeyValue)//right-side dynamic subscript gradually decreases until less than or equal to Keyva is found Lue subscript {rightindex--; } if (Leftindex < Rightindex)//If Leftindex < Rightindex, the value specified by the left and right dynamic subscript is exchanged; when Leftindex==rightindex, jump out of the Ring {temp = Array[leftindex]; Array[leftindex] = Array[rightindex]; Array[rightindex] = temp; }}//When the left and right two dynamic subscripts are equal (i.e., the left and right subscripts point to the same position), you can determine the exact position of the keyValue temp = keyValue; if (temp < Array[rightindex])//When KeyValue < left and right points are pointing at the same value, the keyvalue is exchanged with the value pointed to by RightIndex-1 and returned rightIndex-1 {Array[low] = array[rightindex-1]; ARRAY[RIGHTINDEX-1] = temp; return rightIndex-1;} else//When keyvalue >= a value pointing to the left and right, the keyvalue is exchanged with the value that Rightindex points to and returns Rightindex {array[ Low] = Array[rightindex]; Array[rightindex] = temp; return rightindex; } } }}
Fast sort algorithm (C # implementation)