Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void swap(int *first, int *second){
- int temp=*first;
- *first=*second;
- *second=temp;
- }
- int partition(int *array,int low, int high){
- int pivot=array[low];
- int i=low;
- int j=high;
- while(i<j){
- do{
- i++;
- }while(array[i]<=pivot);
- do{
- j++;
- }while(array[j]>pivot);
- if(j>i)swap(&array[i],&array[j]);
- }
- swap(&array[low],&array[j]);
- return j;
- }
- void quickSort(int *array, int low, int high){
- int j;
- if(low<high){
- j=partition(array,low,high);
- quickSort(array,low,j);
- quickSort(array,j+1,high);
- }
- }
- /*function to print the array*/
- void printArray(int *array, int n){
- for(int i=0;i<n;i++){
- printf("%d ",array[i]);
- }
- }
- int main(int argc,char *argv[]){
- int array[8]={6,4,7,1,8,5,3,2};
- quickSort(array,0,sizeof(array)/sizeof(array[0])-1);
- printArray(array,8);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement