Quick Sort Algorithm C language version

Source: Internet
Author: User

Quick Sort (Quicksort) is an improvement to the bubbling sort . Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts , one part of all the data is smaller than the other part of all the data, and then this method to the two parts of the data are quickly sorted, the entire sorting process can be recursive, To achieve the entire data into an ordered sequence.
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 starting the forward Search (j--), find the first value less than key A[j], will a[j] and A[i] interchange;4) Backward search from I, that is, start backward search (i++), find the first a[i] greater than key], interchange a[i] and a[j];5) Repeat 3rd, 4, until i=j, (3,4 step, did not find the qualifying value, that is, 3 a[j] is not less than the key,4 A[i] 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:Suppose the user enters an array like this:Create a variable i=0 (point to the first data), j=5 (point to the last data), k=6 (assigns the value of the first data). we're going to move all the numbers smaller than K to the left of K, so we can start looking for a number smaller than 6, starting with J, looking from right to left , constantly diminishing the value of the variable J, we find the first subscript 3 of the data is smaller than 6, so the data 3 is moved to subscript 0 position, Move the subscript 0 Data 6 to subscript 3 to complete the first comparison:i=0 j=3 k=6then, to start the second comparison, this time to become bigger than the K, and to go after the search. Sliding scale variable i, found that the data of subscript 2 is the first larger than the K, so with the subscript 2 of the data 7 and J points to the subscript 3 of the data of 6 to exchange, the data state into the following table:i=2 j=3 k=6It is said that the above two comparisons are a cycle. then, the variable J is decremented again, repeating the above cycle comparison. In this example, we do a loop and find I and J "Meet": they all pointto subscript 2. So, the first time the comparison is over. The results are as follows, usually K (=6) the number on the left is smaller than it, the number of K right is larger than it: If I and J do not meet, then sliding scale I find large , not yet, and then diminishing J to find small , so repeated, continuous circulation. Attention to judgment and search is carried out at the same time. then, the data on both sides of the K, and then the process is divided into the above, until no further grouping. Note: The first time quick sort does not directly get the final result, only the number smaller than K and K is divided into the two sides of K. To get the final result, you need to perform this step again on an array of subscript 22 edges, and then decompose the array until the array is no longer decomposed (only one data) to get the correct result. calling Functionsin C + +, you can use the function qsort () to sort the array directly. Usage:void qsort (void *base, int nelem, int width, int (*fcmp) (const void *,const void *));Parameters:1 Array first address to sort2 Number of elements to be sorted in the array3 space size of each element4 A pointer to a function that determines the order of the sortC language version
1 voidSortint*a,intLeftintRight )2 {3     if(left >= right)/*if the index on the left is greater than or equal to the right, it means that a group has been collated.*/4     {5         return ;6     }7     inti =Left ;8     intj =Right ;9     intKey =A[left];Ten       One      while(I < J)/*control in a group to look for again*/ A     { -          while(I < J && Key <=A[j]) -         /*and the search for the end of the condition is, 1, find a less than or greater than the number of keys (greater than or less than depending on you want to ascend the order or Descending) 2, does not match the condition 1, and I and J size is not reversed*/ -         { -j--;/*looking forward*/ -         } +           -A[i] =A[j]; +         /*after finding a number like this, assign it to the value of the previous taken I (if the first loop and key is A A[left], then to key)*/ at           -          while(I < J && Key >=A[i]) -         /*This is I in the group to look forward, ibid, but note the size relationship with the key stop cycle and the opposite, - because the idea of sorting is to throw a number on both sides, the number of left and right sides is the opposite of key.*/ -         { -i++; in         } -           toA[J] =A[i]; +     } -       theA[i] = key;/*return the median key when you've finished searching within the group*/ *Sort (A, left, I-1);/*Finally, the same approach to the left side of the group*/ $Sort (A, i +1, right);/*use the same approach to the group on the right*/Panax Notoginseng                        /*of course there may be a lot of points in the end, until I = J of each group*/ -}

Quick Sort Algorithm C language version

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.