Two implementation ideas of "C-+ +" fast sorting

Source: Internet
Author: User

Method One: Keep pits and determine a value at a time. http://blog.csdn.net/morewindows/article/details/6684558

#include <stdio.h>voidQsortint*array,intLen) {    intvalue, start, end; if(Len <=1)         return; Value= array[0]; Start=0; End= Len-1;  while(Start <end) {          for(; start < end;--end) {             if(Array[end] <value) {Array[start++]=Array[end];  Break; }         }          for(Start < end; + +)start) {             if(Array[start] >value) {Array[end--]=Array[start];  Break; }}} Array[start]=value;    Qsort (array, start); Qsort (Array+start+1, len-start-1 );}intMain () {inta[Ten]={9,8,7,3,4,5,6,1,2,0};qsort (A,Ten);inti; for(i =0; I <Ten; i++) {printf ("%d", A[i]);} }

Method Two: Find two value Exchange http://developer.51cto.com/art/201403/430986.htm at a time

#include <stdio.h>inta[101],n;//define global variables, which need to be used in child functionsvoidQuicksortintLeftintRight ) {     inti,j,t,temp; if(left>Right )return; Temp=a[left];//the base number is stored in temp.I=Left ; J=Right ;  while(i!=j) {//The order is important, start looking from the right.                    while(A[j]>=temp && i<j) J--; //and find the right.                    while(A[i]<=temp && i<j) I++; //swap the position of two numbers in the array                   if(i<j) {T=A[i]; A[i]=A[j]; A[J]=T; }     }     //Finally, the base number is returned to the positiona[left]=A[i]; A[i]=temp; Quicksort (Left,i-1);//proceed to the left, here is a recursive processQuicksort (i+1, right);//proceed to the right, here is a recursive process} intMain () {inti,j,t; //read in Datascanf"%d",&N);  for(i=1; i<=n;i++) scanf ("%d",&A[i]); Quicksort (1, n);//Quick Sort Call//results after sorting the output     for(i=1; i<=n;i++) printf ("%d", A[i]);     GetChar (); GetChar (); return 0; } 

Two implementation ideas of "C-+ +" fast sorting

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.