Initial understanding of "quick sorting" for Algorithms

Source: Internet
Author: User

Fast sorting is also an algorithm. In this sorting method, the speed is relatively fast, basically decreasing by the speed of ㎡, where m is the number of elements in the Set)The core idea is exchangeThe position of a large number and a decimal number.

The main steps are as follows:

1) Prepare an unordered set of at least two elements;

2) Get the initial index starindex, end the index endindex, and obtain the median index middleindex) and the median middle );

3) in the entire arrayBefore and afterFindFirstThe value is greater than the value in the value range, and the position is marked as highindex.From the back to the frontFind the first number that is smaller than the middle value and the number of lower positions lowindex); If highindex <lowindex, the two numbers are exchanged;

4) Next Step (3) continues searching.From the rear of highindexHighindex ++)Start to look for the first number that is larger than the value in the past and later.From the front of lowindex, lowindex --)Start from the back to the front to find the first number smaller than the value. If highindex <lowindex, the two numbers are exchanged until highindex> = lowindex, this round ends.

5) Use highindex and lowindex to reduce the search range and enter the next round. This round of lookup is to cut the entire array into two parts: Part1 [startindex, highindex-1]; Part2 [lowindex + 1, endindex]

Continue to search for the entire array separately as in Steps 3 and 4 until the entire array is fully ordered.

Let's use an instance to understand the magic of fast sorting:

Namespace prj quick sorting {class Program {static void Main (string [] args) {// int [] ary = new int [] {12, 4, 0, 6, 11, 17, 7, 1, 24}; // pass the check. // prepare a random unordered array int [] ary = new int [] {9, 8, 7, 6, 5, 4, 3, 2, 1}; // call the QuickSort function for quick sorting and input the parameter QuickSort (ary, 0, ary. length-1); // traverse the entire array and output the result foreach (int x in ary) {Console. writeLine (x) ;}} static void QuickSort (int [] ary, int startIndex, int endIndex) {// if the initial index is greater than or equal to the end index, end query if (startIndex> = endIndex) {return;} int highIndex = startIndex; int lowIndex = endIndex; // obtain the median int middle = ary using the median index [(startIndex + endIndex) /2]; while (true) {// find the first number greater than the middle value from the beginning to the end, and immediately end the loop for (int I = highIndex; I <= endIndex; I ++) {if (ary [I]> = middle) {// obtain the position highIndex = I; break;} where the first number is larger than the value in the middle ;}} // find the first number smaller than the mean value from the back and then immediately end the loop for (int I = lowIndex; I >= startIndex; I --) {if (ary [I] <= middle) {// obtain the first position lowIndex = I; break;} with a smaller value than the middle value ;}} // if highIndex> = lowIndex, it means that there is no larger value than the value before the value, and there is no longer a smaller value than the value after the value. if (highIndex> = lowIndex) {break;} // int temp = ary [highIndex]; ary [highIndex] = ary [lowIndex]; ary [lowIndex] = temp; lowIndex --; // The next query continues to find the number of highIndex ++ smaller than the value in the previous position; // The next query continues to find a number greater than the value in the last position} // divide the array into two parts for the above process, respectively, exchange until the entire array is ordered QuickSort (ary, startIndex, highIndex-1); QuickSort (ary, lowIndex + 1, endIndex );}}}

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2024043042-0.png "style =" float: none; "title =" capture. PNG1.PNG "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2024043925-1.png "title =" capture. PNG2.PNG "style =" float: none; "/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2024042L8-2.gif "/> A Good sorting method!


This article is from the "Ajax girl" blog. For more information, contact the author!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.