Fast sorting: As the name implies, the sorting method with a relatively fast sorting process may have a large position jump during the sorting process ). 650) this. width = 650; "alt =" j_0018.gif "src =" http://www.bkjia.com/uploads/allimg/131228/202241LL-0.gif "/>
Core Idea of fast sorting: Find the value of the array, find the first value greater than the value from the past to the next, and find the first one smaller than the value from the next to the next, find them and switch the positions. After a round, divide the array into two parts and continue searching.
Procedure:
1) first, give an unordered array, find its value, find the first number that is larger than the value, and then find the first number that is smaller than the value, find and switch the two locations;
2) Judge, if the location of the large number and the decimal number are both in the middle, or the location of the large number is behind the decimal number, the first round ends and the array is divided into two parts;
3) Otherwise, the two numbers will be exchanged, and the position of the large number will be + 1, and the decimal position will be-1; repeat the above steps;
4) when the first round ends, divide the array by startIndex, highIndex-1) lowIndex + 1, endIndex) into two parts, continue to repeat the above steps until the number is sorted;
5) print the sorted array.
Let's take a look at the specific program:
Using System; using System. collections. generic; using System. text; namespace prjquicksort {class Program {static void Main (string [] args) {int [] ary = new int [] {2, 8, 6, 5, 7, 1, 9}; // prepare an unordered array quicksort (ary, 0, ary. length-1); // call the quicksort function foreach (int x in ary) // use the foreach function to print the sorted Array {Console. writeLine (x) ;}} static void quicksort (int [] ary, int startIndex, int endIndex) // quick sorting {// when the starting index is greater than or equal to the real number of the ending index, return directly. If (startIndex> = endIndex) {return;} // The median value is equal to the value at the position of the Start index and the end index. Int middle = ary [(startIndex + endIndex)/2]; // a high index that is larger than the number of indexes in the specified value) is initialized as the starting index. Int highIndex = startIndex; // index with a low index that is smaller than the value in the middle.) The index is initialized to the end index. Int lowIndex = endIndex; // The number of sorting times is uncertain. After multiple sorting times, the array becomes an ordered array. While (true) {// in the array, from the high index to the end index, the first number greater than or equal to the value is searched from the beginning to the end of the index. For (int I = highIndex; I <= endIndex; I ++) {// if the first number greater than or equal to the value is found, the index of the number is given to the high index, and jumps out of the loop. If (ary [I] >= middle) {highIndex = I; break ;}} // In the array, from the low index to the start index, start from the back to the front to find the first number less than or equal to the value. For (int j = lowIndex; j> = startIndex; j --) {// if the first number smaller than or equal to the value is found, the index of the number is assigned to the low index, and jumps out of the loop. If (ary [j] <= middle) {lowIndex = j; break ;}// if the high index is equal to the low index, a loop exists. If (highIndex = lowIndex) {break;} // if the high index is greater than the low index, the loop jumps out. If (highIndex> lowIndex) {break;} // otherwise, the two numbers found are exchanged. Int temp = ary [highIndex]; ary [highIndex] = ary [lowIndex]; ary [lowIndex] = temp; // move the high index one after the next query, the low index moves one place forward. HighIndex ++; lowIndex --;} // cut the array into parts. The first part is from the starting index to the high index-1, and the second part is from the low index + 1 to the ending index. Quicksort (ary, startIndex, highIndex-1); quicksort (ary, lowIndex + 1, endIndex );}}}
Running result: 650) this. width = 650; "title =" QQ20130804233313.png "alt =" 233359883.png" src = "http://www.bkjia.com/uploads/allimg/131228/202241J46-1.png"/>
This article is from the "Lanting drunk beauty" blog and is not reposted!