Advertisement
Guest User

Untitled

a guest
Apr 6th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void swap(int *first, int *second){
  4.  
  5. int temp=*first;
  6. *first=*second;
  7. *second=temp;
  8. }
  9.  
  10. int partition(int *array,int low, int high){
  11. int pivot=array[low];
  12. int i=low;
  13. int j=high;
  14. while(i<j){
  15.     do{
  16.         i++;
  17.     }while(array[i]<=pivot);
  18.     do{
  19.         j++;
  20.     }while(array[j]>pivot);
  21.  
  22.     if(j>i)swap(&array[i],&array[j]);
  23. }
  24. swap(&array[low],&array[j]);
  25. return j;
  26. }
  27.  
  28. void quickSort(int *array, int low, int high){
  29. int j;
  30. if(low<high){
  31.     j=partition(array,low,high);
  32.     quickSort(array,low,j);
  33.     quickSort(array,j+1,high);
  34. }
  35.  
  36.     }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. /*function to print the array*/
  43.  
  44. void printArray(int *array, int n){
  45.  
  46. for(int i=0;i<n;i++){
  47.     printf("%d ",array[i]);
  48.     }
  49. }
  50.  
  51. int main(int argc,char *argv[]){
  52.  
  53. int array[8]={6,4,7,1,8,5,3,2};
  54. quickSort(array,0,sizeof(array)/sizeof(array[0])-1);
  55. printArray(array,8);
  56.  
  57.  
  58. return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement