Skip to content

Instantly share code, notes, and snippets.

@kimitoboku
Created May 9, 2014 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimitoboku/cc012b663a6809c35deb to your computer and use it in GitHub Desktop.
Save kimitoboku/cc012b663a6809c35deb to your computer and use it in GitHub Desktop.
quick sort
#include<stdio.h>
int Partition(int A[],int left,int right){
int x = A[left];
int i = left;
int j;
for(j=left+1;j<=right;j++){
if(A[j] < x ){
i++;
int buf = A[i];
A[i] = A[j];
A[j] = buf;
}
}
int buf = A[i];
A[i] = A[left];
A[left] = buf;
return i;
}
void qsort(int A[],int left,int right){
if(left >= right){
return;
}else{
int middle = Partition(A,left,right);
qsort(A,left,middle-1);
qsort(A,middle+1,right);
}
}
int main(void){
int n;
int array[100];
scanf("%d",&n);
int i;
for(i=0;i<n;i++){
scanf("%d",&array[i]);
}
qsort(array,0,n-1);
for(i=0;i<n;i++){
printf("%d ",array[i]);
}
puts("");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment