Sit on the toilet. Algorithm: Quick Sort

Source: Internet
Author: User

Initial state: 6 1 2 7 9 3 4 5 10 8

Process diagram for sorting

First Sentinel J started out. Because the number of datums set here is the leftmost number, it is important to have Sentinel J go first ( think about why ):: The last and the base number must be less than the number of the base number,

#include <cstdio>#include<algorithm>using namespacestd;ints[ -],n;voidQuick_sort (intLeftintright) {//the interval sort of left--right    if(Left>=right)return; intTm=s[left];//Base Number    inti=left,j=Right ;  while(i!=j) {         while(s[j]>=tm&&i<j) j--;//look to the left from the right .         while(s[i]<=tm&&i<j) i++;//look from left to right        if(i<j) Swap (S[i],s[j]);    } swap (s[left],s[i]); Quick_sort (Left,i-1); Quick_sort (i+1, right);}intMain () {//freopen ("data.in", "R", stdin);scanf"%d",&N);  for(intI=1; i<=n;i++) scanf ("%d", s+i); Quick_sort (1, n);//for (int i=1;i<=n;i++) printf ("%d", s[i]);//printf ("\ n");}

Sit on the toilet. Algorithm: Quick Sort

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.