Created
May 9, 2014 13:48
-
-
Save kimitoboku/cc012b663a6809c35deb to your computer and use it in GitHub Desktop.
quick sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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