C # Quick Sort

Source: Internet
Author: User
C # Quick Sort

Using system;using system.collections.generic;using system.linq;using system.text;namespace Sort{class QuickSorter        {private static int[] MyArray;        private static int arraySize;            public static int[] Sort (int[] a) {arraySize = A.length;    QuickSort (A, 0,arraysize-1);        return A;            } private static void QuickSort (int[] myArray, int left, int. right) {int I, j, S;                if (left < right) {i = left-1;                j = right + 1;                s = myarray[(i + j)/2];                    while (true) {while (Myarray[++i] < s);                    while (Myarray[--j] > s);                    if (i >= j) break;                Swap (ref myarray[i], ref myarray[j]);                } QuickSort (MyArray, left, i-1);            QuickSort (MyArray, J + 1, right);      }        }  private static void Swap (ref int left, ref int. right) {int temp;            temp = left;            left = right;        right = temp; }    }}

The idea of quick sequencing:

Set to sort the array is a[0] ... A[n-1], the first arbitrary selection of data (usually the first number of the array) as the key data, and then all the smaller than the number of it in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip. It is important to note that fast sorting is not a stable sorting algorithm, that is, the relative position of multiple identical values may change at the end of the algorithm.

A quick sort of algorithm is:

1) Set two variables I, J, at the beginning of the order: i=0,j=n-1;

2) with the first array element as the key data, assign the value to key, i.e. key=a[0];

3) Forward search from J, that is, after the start of the forward search (j--), find the first value less than key a[j], the value of key keys and a[j] exchange;

4) Backward search from I, that is, to start backward search (i++), find the first a[i] greater than key], and the value of key to A[i] exchange;

5) Repeat the 3rd step

6) Repeat the 3rd, 4, and 5 steps until i=j; (3,4 step, did not find the qualifying value, that is, 3 a[j] is not less than key,4 a[j] is not larger than the time to change the value of J, I, so j=j-1,i=i+1, until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).

Example:

Taking an array as an example, the first number of intervals is the base count.

i = 3; j = 7; x=72

Repeat the above steps, looking forward from behind, then looking backwards.

Start looking forward from J, when j=5, meet the conditions, will a[5] dug into the last pit, a[3] = a[5]; i++;

From I start looking backwards, when i=5, due to i==j exit.

At this point, I = j = 5, and a[5] is just the last hole dug, so X is filled into a[5].

The array becomes:

The above is the C # Quick sort of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.