Created
January 8, 2013 22:45
-
-
Save CaglarGonul/4488735 to your computer and use it in GitHub Desktop.
Quicksort in C
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> | |
#include <stdlib.h> | |
void printArray(int *array,int n){ | |
int i; | |
for(i=0;i<n;i++){ | |
printf(" %d " , array[i]); | |
} | |
printf("\n"); | |
} | |
int PARTITION(int *A,int p,int r){ | |
int x = A[p]; | |
int i = p-1; | |
int j = r+1; | |
while(1){ | |
do{ | |
j=j-1; | |
}while(A[j]>x); | |
do{ | |
i=i+1; | |
}while(A[i]<x); | |
if(i<j){ | |
int temp=A[i]; | |
A[i]=A[j]; | |
A[j]=temp; | |
}else{ | |
return j; | |
} | |
} | |
} | |
void QUICKSORT(int *A, int p, int r){ | |
if(p<r){ | |
int q = PARTITION(A,p,r); | |
QUICKSORT(A,p,q); | |
QUICKSORT(A,q+1,r); | |
} | |
} | |
int main() | |
{ | |
int a[12] ={5,3,2,6,4,1,3,7,90,72,80,102}; | |
printArray(a,12); | |
QUICKSORT(a,0,11); | |
printArray(a,12); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment