Quick Sort
time limit (normal/java):
ms/
MS
Run memory limit: 65536 KB
Contest description
Given the number of input ordering elements n and the corresponding n elements, write out the program, using the internal sorting algorithm in the fast sorting algorithm to sort, and output the corresponding sequence of sorting the final result.
Input
A total of two lines, the first row gives the number of ordered elements N, the second row gives n elements, 1≤n≤100000, each element has a value range of [0,100000]
Output
A row that outputs the sort result.
Sample input
7
48 36 68 72 12 48 2
Sample output
2 12 36 48 48 68 72
Code:
#include <cstdio>
#include <algorithm>
using namespace Std;
void swap (int &a,int &b)
{
int t=a;a=b;b=t;
}
int partition (int a[],int l,int R)
{
int i,j;
i=l,j=r+1;;
while (1)
{
while (I<=r&&a[++i]<a[l]);
while (J>=l&&a[--j]>a[l]);
if (i>=j) break;
Swap (a[i],a[j]);
}
Swap (a[l],a[j]);
Return J;
}
void Qsort (int a[],int l,int R)
{
if (r<=l) return;
int j=partition (A,L,R);
Qsort (a,l,j-1);
Qsort (A,J+1,R);
}
int main ()
{
int i,n;
scanf ("%d", &n);
int *p= (int*) malloc (sizeof (int) * (n+1));
for (i=1;i<n+1;i++)
scanf ("%d", &p[i]);
Qsort (P,1,n);
for (i=1;i<=n;i++)
if (i<n)
printf ("%d", p[i]);
Else
printf ("%d\n", P[i]);
return 0;
}
Noj 1064 Quick Sort