Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int low(int a, int b){
  5.     if (a < b){        
  6.         return 1;      
  7.     } else if (a > b){
  8.         return -1;    
  9.     } else {           
  10.         return 0;    
  11.     }
  12. }
  13.  
  14. void quicksort(int* arr, int c){    
  15.     int l = c; int k = 0;
  16.     int midle = arr[c/2];          
  17.     while (k <= c){                
  18.         while (low(arr[k], arr[midle]) == 1)
  19.             k++;                             
  20.        
  21.         while (low(arr[c], arr[midle]) == -1)
  22.             c--;
  23.            
  24.         if (k <= c){                   
  25.             swap(arr[k++], arr[c--]);  
  26.                                        
  27.         }
  28.     }
  29.    
  30.    
  31.            
  32.     if (c > 0)                 
  33.         quicksort(arr, c);  
  34.        
  35.     if (l > k)  
  36.         quicksort(arr + k, l - k);  
  37.                                      
  38.    
  39. }
  40.  
  41.  
  42. int main() {
  43.    
  44.     int arr[20] = {6, 5, 4, 4, 2, 1, 3, 1, 23, 1, 2, 4, 32, 54, 45, 22, 7, 46, 99, 47};
  45.     for(int i = 0; i < 20; i++){
  46.         cout << arr[i] << " ";
  47.     } cout << endl;
  48.     quicksort(arr, 19);          
  49.     for(int i = 0; i < 20; i++){
  50.         cout << arr[i] << " ";
  51.     }
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement