Advertisement
apl-mhd

QuickSort

Feb 21st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4.    
  5. void quickSort(int a[], int f, int l){
  6.        
  7.     int i,j,temp;
  8.             if(f<l){               
  9.             i=f+1;
  10.             j=l;           
  11.                
  12.             while(i<=j){                       
  13.                 while(a[i]<=a[f]) i++;
  14.                    
  15.                 while(a[j] >=a[f]){    
  16.                    
  17.                     if(i>j)
  18.                         break;
  19.                      j--;
  20.                      
  21.                 }
  22.             printf("i =%d j=%d\n",i,j);
  23.             if(i<j){
  24.            
  25.                 temp = a[i];
  26.                 a[i] = a[j];
  27.                 a[j] = temp;
  28.                 }    
  29.             }
  30.            
  31.             temp = a[f];
  32.             a[f] = a[j];
  33.             a[j] = temp;
  34.            
  35.             quickSort(a,f,j-1);
  36.             quickSort(a,j+1,l);
  37.                
  38.         }
  39.        
  40.    
  41. }
  42.  
  43. int main(int argc, char **argv)
  44. {  
  45.     int number[11] = {11,10,9,8,7,6,5,4,3,2,1};
  46.    
  47.     quickSort(number,0,10);
  48.    
  49.     for(int i=0; i<11; i++){
  50.        
  51.         printf(" %d", number[i]);
  52.        
  53.         }
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement