C # Quick Sorting Algorithm Basics

Source: Internet
Author: User

I believe that algorithms are a major difficulty for many developers. The reason why they are difficult is that, just like the design pattern, many people do not have a good understanding after reading them and do not want to operate on them, I only stayed on the theoretical study and gradually forgot over time. Some things can be invented, but some things can either be memorized or understood and practiced to consolidate. It is useless to stick to the story of development. It is still a little bit difficult to understand. The best way is to break into the Code on the basis of understanding, so that you can know and understand it. This article is just a brief introduction to the quick sorting algorithm. You can give some advice, but please kindly advise: the quick sorting algorithm defines quick sorting (Quicksort) is an improvement for the Bubble sorting. Proposed by C. A. R. Hoare in 1962. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence. Fast sort algorithm video dance here can look at a foreigner's great creative video to understand the fast sort algorithm is like (http://v.youku.com/v_show/id_XMzMyODk4NTQ4.html ), it vividly reproduce the fast Sorting Algorithm in the form of dancing. The Quick Sort Algorithm assumes that you need to use a Quick Sort Algorithm to sort an existing set of integer data (for example, int [] unsort = {3, 2, 4, 1, 7, 5}, assuming that the first data 3 is used as the number of left and right data partitions), then when int midPosition = QuickSort (unsort, left, right) is run, the result of the obtained array is: {2, 1, 3, 4, 7, 5}, and the midPosition returns the index location of Data 3 (that is, index 2 ). In this way, the data can be divided into {2, 1}, {4, 7, 5} parts according to 3 as the number of separation (the order of 3 has been sorted, then, sort by recursion. I think the following code is very understandable. Although there are many improvements on the Internet, this is not the focus of this article. This article is just an entry-level content. It is easy to understand and try to be as simple as possible. After all, it is more complicated and I cannot write it out. The only thing that is hard to understand is how to understand the positions of separated numbers. For me, I have never learned well before in college, so that when I come to study later, I will always be preemptible and feel so difficult. If a person is at odds, doing things is always half the result. So after I spend most of my time understanding the location of the number of separation, the following fast sorting algorithm can be manually typed. After understanding it, You can consolidate the learned knowledge. Next, let's talk about my understanding of how to locate the number separation. (1) determine the number of separators in advance. The default value is the first number (for example, instance 3, we assume that the number is sorted in ascending order) (2) sort by the right and left ends in the middle. (a single sort is only used to sort the separated numbers, and the data that separates the left part of the number is smaller than the data in the right part ). The right side starts sorting. If the data score on the right side is large, the position does not need to be changed. Once the data score on the right end is several hours, you need to move the small data on the right end to the left. For details, refer to the following steps. In addition, the sorting direction starts from the left end. If the left end data score is an hour, the position does not need to be changed. Once the left-side data score is large, you need to move the large number of left-side data to the right. In addition, the sorting direction is changed to the right position that has just arrived and then sorted. To put it bluntly, at the beginning, we took a number from the left and saved it to a variable (separated number), which is equivalent to leaving a blank space on the left, let the number on the right of the number of separation (This number is smaller than the number of separation) to store. Once it is stored, a blank space is displayed on the right, wait for the number on the left side of the split number (This number is greater than the split number) to store it. It will be adjusted all the time. If it is not adjusted, the remaining space will be the split number. Step: {3, 2, 4, 1, 7, 5} the first small step: Assign 3 to a stored variable, suppose it is int splitNum = 3 (and the position of array 3 will be occupied by a number smaller than 3 ). We can understand that the number of array 3 is currently a vacant space: {X, 2, 4, 1, 7, 5}, X represents the second small step of vacant space: compare splitNum with 5 on the right (what is the purpose of comparison with the right end? Of course, we want to keep the number greater than 3 to the right of 3, A number smaller than 3 is placed on the left of 3), 5 is big, so the right side of 5 remains unchanged: {X, 2, 4, 1, 7, 5} The third small step: compare the value of splitNum with the value of 7 on the right. The value is 7, so the value on the Right of 7 remains unchanged: {X, 2, 4, 1, 7, 5}. The fourth step is: compare splitNum with 1 on the right. splitNum is large, so the value of X is 1 (then the index position of the original array 1 is a blank): {1, 2, 4, x, 7, 5} The Fifth small step: Compare splitNum with 1 on the left end (what is the purpose of comparison with the left end, of course, you want to put a number greater than 3 to the right of 3, and a number smaller than 3 to the left of 3). splitNum is large, so 1 remains unchanged: {1, 2, 4, X, 7, 5} Six small steps: Compare splitNum with 2 on the left. splitNum is large, SO 2 remains unchanged: {1, 2, 4, X, 7, 5}. The seventh small step: compare splitNum with 4 on the left. 4 is big, so X is assigned 4 values (then the index location of the original Array 4 is blank): {1, 2, X, step 8 of 4, 7, 5}: Since the comparison is complete in two days, the value of X assigned to splitNum is {1, 2, 3, 4, 7, 5} separated by 3. The numbers on the left of 3 are smaller than those on the Right of 3 (sorted in ascending order) copy the code of the Quick Sort Algorithm to namespace Sort {class Program {static void Main (string [] args) {string result = string. empty; int [] unsort = {3, 2, 4, 1, 7, 5}; result = GetPrint (unso Rt); Console. write ("Result Before sorting:"); Console. writeLine (result); // quick sorting of QuickSort (unsort, 0, unsort. length-1); result = GetPrint (unsort); Console. write ("sorting result:"); Console. writeLine (result); Console. readLine ();} /// <summary> /// call the fast Sorting Algorithm /// </summary> /// <param name = "unsort"> integer array to be sorted </param> /// <param name = "left"> Start Point on the left </param> /// <param name = "right"> end point on the right </param> public static void QuickSort (int [] Unsort, int left, int right) {if (left <right) {// obtain the intermediate index position of a sort: int midPosition = GetSplitNum (unsort, left, right ); // implement QuickSort recursively (unsort, left, midPosition-1); QuickSort (unsort, midPosition + 1, right );}} /// <summary> /// obtain the intermediate index position of a sort. /// </summary> /// <param name = "unsort"> integer array to be sorted </param> /// <param name = "left"> Start Point on the left </param> /// <param name = "right"> end point on the right </param> public static int G EtSplitNum (int [] unsort, int left, int right) {int splitNum = unsort [left]; while (left <right) {/*** compare from the right end * (1) if the number of scores from the right end is large, no processing is required * (2) if the distance from the right side is small, you need to move it to the left side of the separator line **/while (left <right & splitNum <= unsort [right]) {right --;} unsort [left] = unsort [right];/*** start from the slave * (1) if the number of data scores from the left end is small, you do not need to process it. * (2) If the left-side data score is large, you need to move it to the right of the separator line **/while (left <right & splitNum> = unsort [left]) {Left ++;} unsort [right] = unsort [left];} // after a comparison, the positions of the separated numbers can be determined. unsort [left] = splitNum; return left ;} /// <summary> /// print the output result /// </summary> /// <param name = "unsort"> data </param> public static string GetPrint (int [] unsort) {string result = string. empty; foreach (int n in unsort) {if (! String. isNullOrEmpty (result) {result + = string. format ("-> {0}", n);} else {result = string. format ("{0}", n) ;}} return result ;}}}

Related Article

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.