Step-by-Step to explain the fast sorting algorithm and C # version of the implementation of the example _c# tutorial

Source: Internet
Author: User

Algorithm idea
The fast sort is a sort of division Exchange C.r.a.hoare in 1962. It adopts a strategy of divide and conquer, which is often referred to as the Partition method (Divide-and-conquermethod).
The basic idea of the method is:
1. First, take a number out of the series as the base number.
2. The partitioning process, which places the number larger than this to its right, is less than or equal to its number on the left.
3. Repeat the second step to the left and right intervals until the interval is only one number.
Although a quick sort is called a divide-and-conquer method, the three-word divide is clearly not a good way to generalize all the steps of a quick sort. So my quick sort is further explained: digging pit filling number + partition method:
Let's take a look at the example, and give the definition below (it's best to summarize the definition in your own words, which is helpful for implementing the code).
Take an array as an example, taking the first number of intervals as the base number

At the beginning, i = 0;   j = 9; X = A[i] = 72
Since the number in a[0 has been saved to X, it can be understood that a hole has been dug in the array a[0] and other data can be populated here.
Start with J to find a number that is smaller or equal to x than X. When the j=8, meet the conditions, will a[8] dug and then fill in the last pit a[0]. A[0]=A[8];  i++; Such a pit a[0] was taken care of, but formed a new pit a[8]. Simple, then find the number to fill a[8] this pit. This time from I start looking backwards for a number greater than X, when i=3, in line with the conditions, will a[3] dug again fill in the last pit a[8]=a[3]; j--;
Array into:

i = 3;   j = 7; x=72
Repeat the above steps, looking forward from behind, and looking backwards in the past.
Start looking forward from J, when j=5, meet the conditions, will a[5] dug into the last pit, a[3] = a[5]; i++;
Start looking backwards from I, when i=5, as I==j exits.
At this point, I = j = 5, and a[5] happens to be the last pit dug, so the x is filled in a[5].
Array into:

You can see that the number in front of a[5] is smaller than it, and the number after a[5] is greater than it. So again to a[0 ... 4] and a[6 ... 9] These two sub ranges repeat the above steps.
Summarize the number of digging pits
1. I =l; j = R; Dig up the base number to form the first pit a[i].
2. j--from behind to find the number of smaller than it, found after digging this number to fill a pit a[i].
3. i++ from the front to find a larger number than it, found also dug this number to fill in the previous pit a[j].
4. Repeat 2, 32 steps until I==j, and fill in a[i].

C # Implementation Example

Namespace QuickSort {public class Quicksortclass {public int Division (list<int> List, int left, int right
      {////First select a datum element int basenum = List[left];  while (R < right) {//starts looking forward from the right-hand end of the array until you find a number that is smaller than base (including base equivalents) while (left < right &&
        List[right] >= basenum) right = right-1;
        Finally found a smaller element than Basenum, the thing to do is to place this element in the base position list[left] = List[right]; Look backwards from the left end of the array until you find a number that is larger than the base (including the base equals number) while (left < right && List[left] <= basenum) lef
        T = left + 1;
      Finally found a larger element than Basenum, the thing to do is to put this element to the final position list[right] = List[left];
      //Finally, put the basenum in the left position list[left] = Basenum;
    In the end, we found that the left side of the value part is smaller, the right side of the left-hand position is larger The public void QuickSort (list<int> List, int left, int right) {//subscript must be less than the subscript, otherwise it will be beyond the IF ;
right) {/////array to split, take out the next split Datum label        int i = Division (list, left, right);

        A recursive cut of a set of values to the left of the "datum label" so that the values are sorted QuickSort (list, left, i-1);
      A recursive cut of a set of values to the right of the "datum label" so that the values are sorted QuickSort (list, i + 1, right-hand);
 }
    }
  }
}

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.