Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- void swap(int *a,int *b){
- int t=*a;
- *a=*b;
- *b=t;
- }
- int partition(int A[],int l,int h){
- int i=l-1,j=h+1,pi=rand()%(h-l+1)+l;
- while(1){
- while(i<h && A[++i]<A[pi]);
- while(j>l && A[--j]>A[pi]);
- if(i>=j) return j;
- swap(&A[i],&A[j]);
- }
- }
- void quick_sort(int A[],int l,int h){
- if(l<h){
- int p=partition(A,l,h);
- quick_sort(A,l,p);
- quick_sort(A,p+1,h);
- }
- }
- int main(){
- int n;
- printf("Enter number of elements: ");
- scanf("%d",&n);
- int A[n];
- printf("Enter the elements: ");
- for(int i=0;i<n;i++) scanf("%d",&A[i]);
- quick_sort(A,0,n-1);
- for(int i=0;i<n;i++) printf("%d ",A[i]);
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement