Quick-Sort Learning in C #

Source: Internet
Author: User

Recently read a word, said in the real life, will write the person does not necessarily write good article, learning programming language is like to learn to write, learn programming language does not necessarily can write good program.

I think it is very reasonable, before reading, the Basic Learning C # Grammar knowledge, is into a door, but one to write the program is no clue, do out of the program is like a primary school diary, just use some simple API to put together the function, no logic, no beauty elegant.

So I decided to slowly start learning algorithmic knowledge, although I am poor in mathematics, logical ability to the pole, look at the code of these algorithms to see me is the mentality of explosion, really scalp numbness, only long-term persistence study, 1.1 points of slow progress.

Fast sorting is a kind of common sorting algorithm in divide and conquer method, which mainly uses recursive solution.

The idea of an algorithm is to divide an array into sub-arrays that are less than or equal to the base number and a sub-array that is greater than the base number, and then, by recursive invocation, sort the two sub-arrays, until the array element has only 0 or 1 elements, stops the recursion, and then adds each of the ordered sub-arrays, and finally gets

The steps are as follows:

1. Select a reference value

2. Divide the array into two sub-arrays: elements less than the base value and elements greater than the base value

3. Sort the two sub-arrays

Then repeat the 3 steps for the Subarray until the sub-array is not decomposed (only 0 or 1 elements of the Subarray)

The above procedure can be repeated using recursive functions, which must meet 2 conditions in a recursive function

1. Baseline conditions--when a condition is met, the function does not recursively return the call itself

2. Recursive condition--satisfies the recursive condition to call itself function to recursively

The book I read is an example of a quick sort implemented using Python code, with the following code:

1 defquicksort (array):2     ifLen (Array) < 2:#baseline condition: An array that is empty or contains only one element is "ordered"3         returnArray4     Else:#Recursive conditions5Pivot =Array[0]6less = [i forIinchArray[1:]ifi<= Pivot]#an array of all elements less than or equal to the base value7Greater = [I forIinchArray[1:]ifi> Pivot]#a sub-array of all elements that are greater than the base value8         returnQuicksort (less) + [pivot] +quicksort (Greater)9 Ten  One Print(Quicksort ([10, 5, 2, 3]))

In this code, when a recursive condition is first met, a base number is obtained, the base number is usually the first element of the array, then divided into two sub-arrays, a sub-array that is less than or equal to the base number, and a sub-array that is greater than the base number, and recursively calls the two sub-arrays, and finally gets the sorted

After reading the book, the mind will soon have thoughts, but when I want to convert the above code into C #, I can not touch the brain, hit the skull is not able to, because in C # can not be as simple as the above implementation, the main thing is C # in the array can not be combined by the direct addition.

So I read a lot of C # quick sort in the Internet after the detailed description of the following code:

1         /// <summary>2         ///quickly sort an array in ascending order3         /// </summary>4         /// <param name= "Array" >the array to sort</param>5         /// <param name= "Leftindex" >The index of the first number traversed from the left</param>6         /// <param name= "Rightindex" >Index of the first number traversed from the right</param>7          Public Static voidQuickSort (int[] Array,intLeftindex,intRightindex)8         {9             if(Leftindex >= Rightindex)//Baseline conditionsTen                 return; One             intn =Leftindex; A             intm =Rightindex; -             intPivot =Array[leftindex]; -              while(n! =m) the             { -                  for(; n < m; m--) -                 { -                     if(Array[m] <pivot) +                     { -Array[n] = array[m];//Swap Location +                          Break; A                     } at                 } -                  for(; n < m; n++) -                 { -                     if(Array[n] >pivot) -                     { -ARRAY[M] =Array[n]; in                          Break; -                     } to                 } +             } -Array[n] = pivot;//after the completion of the cycle has been in accordance with less than the base number and the number of more than the baseline sequence, the base number in the middle.  theQuickSort (array, Leftindex, N-1);//recursive to two sub-arrays *QuickSort (array, n +1, Rightindex); $}

The idea of a fast online sequencing, even if the baseline conditions and what I see in the book are completely different, and the logic in the book is not the same, but still the division of the law to solve the problem.

Not only to separate two sub-arrays, but to sort the current array, sorting process is not good understanding, from right to left loop, find a number larger than the base number, and then from left to right loop, find out the number is smaller than the base number, and then the two digits to the position, to the last only a number of time, that is the base number Put the base number in this position.

So I think for 2 days, to be able to independently write the above quick sort code, but I do not know whether I really really understand the sort of thinking, or because the look too long, has put the sort of way back in the brain, it is sad, smart small partners will be able to learn, I've been thinking about it for two days and I'm not sure I want to know.

Finally, I still have a question, the time complexity of fast sorting is O (nlogn), then the above Python code is still O (NLOGN)?

New declaring arrays, and then assigning them, and then merging them, does that affect the complexity of time?

Even I want to use LINQ syntax

1             int [] less = (from inwhereselect  i). ToArray (); 2             int [] Greater = (from inwhereselect i). ToArray ();

The returned array is even in order, so is it not easier for me to add the two arrays and the base number directly? I have been completely confused, do not know how the complexity of the time is how to calculate, learning the long road is really long ah!

Quick-Sort Learning in C #

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.