04 Quick Sort (code blank)

Source: Internet
Author: User

Sorting is often used in a variety of situations.
Fast sequencing is a very common and efficient algorithm.

The idea is: choose a ruler First,
Use it to pass the whole queue through the sieve,
To ensure that the left element is not greater than it, and that the element on its right is not less than it.

In this way, the sorting problem is divided into two sub-intervals.
It is possible to sort the sub-intervals separately.

The following code is an implementation, please parse and fill in the missing code in the dash section.

#include <stdio.h>voidSwapintA[],intIintj) {    intt =A[i]; A[i]=A[j]; A[J]=t;}intPartitionintA[],intPintR) {    inti =p; intJ = R +1; intx =A[p];  while(1){         while(I<r && a[++i]<x); When while () is stopped, a[i]>x, or i>=r, is all a[i]<x while(a[--j]>x); When while () is stopped, the A[j]<xif(I&GT;=J) Break; Abnormal case, then there must be a[j]<a[i], at this time a[i] is still greater than x,x position in the p,p on J left, also on I left.
Swap (A,I,J); Normal conditions I<j<r, a[i] and A[j] exchange, x position unchanged} ______________________; Therefore, fill in the Swap (A,P,J)
returnJ;}voidQuicksortintA[],intPintR) { if(p<R) { intQ =partition (A,P,R); Quicksort (A,p,q-1); Quicksort (A,q+1, R); }} intMain () {inti; intA[] = {5, -,6, -,2,8, +, -,6, A,1, -}; intN = A; Quicksort (A,0, N-1); for(i=0; i<n; i++) printf ("%d", A[i]); printf ("\ n"); return 0;}

Note: Fill in only what is missing, and do not write any existing code or descriptive text on any of the surfaces.

04 Quick Sort (code blank)

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.