Title Description
Use the fast sort algorithm to output the number of read-in n from small to large.
Fast sequencing is one of the necessary algorithms for informatics competitions. For the quick sorting is not very familiar with the students can self-surfing the internet to find relevant information, master after the independent completion. (c + + players Please do not try to use the STL, although you can use sort over and over, but you do not grasp the essence of the fast sorting algorithm.) )
Input/output format
Input format:
The 1th behavior of the input file sort.in a positive integer n, the 2nd line contains n spaces separated by a positive integer a[i], for the number you need to sort, the data is guaranteed a[i] not more than 1000000000.
Output format:
Output file Sort.out the given n number from small to large output, the number of spaces between the lines, line end of line and no space.
Input and Output sample input sample #: Copy
54 2 4) 5 1
Output Example # #: Replication
1 2 4) 4 5
Description
For 20% of the data, there are n≤1000;
For 100% of the data, there is n≤100000.
"Code":
Std::ios::sync_with_stdio (FALSE);
Std::cin.tie (0);//cin optimization
#include <bits/stdc++.h>using namespacestd;inta[100000];//because the maximum value of the input number is 100,000. voidQsortintLeftintRight//where to start searching and where to search{ if(Left==right)return;//if they're equal to each other, end intmid=a[(left+right)/2];//take the middle value intI=left;//I instead of left intJ=right;//J instead of right while(I<=J)//I look from the left { while(A[i]<mid) i++;//to find a smaller than the middle number. while(A[j]>mid) j--;//big. if(I<=J)//to write! We can't judge until this cycle is over . { inttmp=A[i]; A[i]=A[j]; A[J]=tmp; I++;j--;}//Exchange } if(left<j) qsort (LEFT,J);//one cycle is not enough, because it is guaranteed that the next recursion will end after J. So this is just a loop ahead of you. if(i<right) qsort (i,right);//the back of the loop. return;//it's over.}intMain () {Std::ios::sync_with_stdio (false); Std::cin.tie (0); intN; CIN>>n;//number for(intI=0; i<n;i++) cin>>a[i];//inputQsort0, N-1);//Loop, which starts from zero. for(intI=0; i<n;i++) cout<<a[i]<<" ";//Outputcout<<endl;//end. }
View Code
#include <bits/stdc++.h>using namespacestd;intMain () {multiset<int>sort;//Create a new red and black tree intN; scanf ("%d",&N); for(intI=1; i<=n;i++) { intA; CIN>>A; Sort.insert (a);//inserting data into a red-black tree } Set<int>:: Iterator it; for(It=sort.begin (); It!=sort.end (); it++) cout<< *it<<' ';//traverse the output red-black tree, which is the sort resultcout<<Endl; return 0;}
View Code 2
Rokua P1177 "Templates" quick sort "quick sort/multiset Sort"