C # Quick Sort algorithm

Source: Internet
Author: User

Today, the next ranking algorithm, including the bubble sorting method and direct sorting method, these are relatively simple, but the rapid sorting method is difficult, so the focus of research.

First say the principle: the rapid sorting method is the recursive way to treat the order of the sequence of several operations, each operation so that the number of parts of the operation of an element is divided into two parts, part is less than the cutoff value, the other part is greater than the cutoff value. The cutoff value is generally referred to as the "pivot". In general, the first number on the left as the cutoff value, the series according to the dividing value into the left and right parts, the left part is less than the cutoff value, and then the lower part of the cutoff value, and then to the left and right two parts of the operation, until the final completion of the sorting.

Take the sequence 14,11,25,37,9,28 as an example to describe in detail the algorithm for performing a fast sequencing:

1, select the pivot of the column to be sorted, usually with the first element of the sequence as the pivot. In this sequence, we select the first element 14 as the pivot, Npivot = 14.

2, set two pointers I and J, respectively, pointing to the first and the end of the sequence of elements. I point to the first element, J points to the tail element 28. as follows:

3. Move the tail pointer J forward so that it points to the element that calculates the first small Yu Yu axis (that is, 14) from the tail of the sequence and replaces the element with the position that the head pointer I points to. _narray[i] =_narray[j]. as follows:

The value at the point of the I pointer is actually the value of the pivot at the first execution of the operation, where the operation can be understood as the value at which the I pointer points is already displaced to the pivot, at which point I is already an empty space, where the element found less than the pivot is filled in.

4. Move the head pointer back to the first element that is greater than the pivot (that is, 14) from the head of the series and displace the element to the position where the tail pointer J points. _narray[j] =_narray[i]. as follows:

It can also be understood here that the value of the J Pointer Point has been displaced in the previous operation. J is already an empty space.

5, repeat steps 3 and 4 so that you end the cycle until i==j.

6, after exiting the loop, I and J must point to the same position. In the position of the front element, its value is less than the pivot. At the back of the position, the value is greater than the pivot. It is obvious that the position I and J points at the same time should be the "new home" of the pivot. _narray[i]=npivot.

At this point, a trip to the end of the sort. The first element of the column to be sorted divides the sequence into two parts that are smaller and larger than its size. For example:

Next, we perform the same sort operation for this big two-part series.

So "recursive" until the entire sequence is finished sorting operations.

The following is the C # implementation code

  

        static void Main (string[] args) {Console.WriteLine ("Enter the column to be sorted (in \", \ "split):");            String _s = Console.ReadLine (); string[] _sarray = _s.split (",".            ToCharArray ());            int _nlength = _sarray.length;            int[] _narray = new Int[_nlength];            for (int i = 0; i < _nlength; i++) {_narray[i] = Convert.ToInt32 (_sarray[i]);            } var list = _narray.tolist ();            QuickSort (list, 0, _nlength-1);            foreach (var i in list) {Console.WriteLine (i.ToString ());            } while (true) {Thread.Sleep (10000);             }}//Gets the pivot position left and right after pivot value private static int division (list<int> List, int right, int. right) { while (leave < right) {int num = List[left];//The first element as a pivot if (num &G T                 List[left + 1]) {   List[left] = list[left + 1];                    List[left + 1] = num;                left++;                    } else {int temp = List[right];                    List[right] = list[left + 1];                    List[left + 1] = temp;                right--; } Console.WriteLine (String.            Join (",", list));            } Console.WriteLine ("--------------\ n"); return left;            The position of the pivot at this point} private static void QuickSort (list<int> List, int left, int. right) {                if (left < right) {int i = Division (list, left, right);                Sort the left part of the pivot QuickSort (list, i + 1, right);            Sort the right part of the pivot QuickSort (list, left, i-1);     }        }

  

C # Quick Sort algorithm

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.