kilolilo

Untitled

Nov 30th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. void sort(int *a,int n){
  7.     if (n>1){
  8.         int l=0,r=n - 1;
  9.         int c=a[rand()%n];
  10.         do{
  11.             while(a[l]<c){
  12.                 l++;
  13.             }
  14.             while(a[r]>c){
  15.                 r--;
  16.             }
  17.  
  18.             if (l<=r){
  19.                 swap(a[l++],a[r--]);
  20.             }
  21.         } while(l <= r);
  22.         sort(a,r+1);
  23.         sort(a+l,n-l);
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     int n=7;
  30.  
  31.     while(true) {
  32.         cin >> n;
  33.         int a[n];
  34.  
  35.         for(int i = 0; i < n; i++ ){
  36.             a[i] = rand() % 100;
  37.         }
  38.         sort(a,n);
  39.         for(int i=0;i<n;i++){
  40.             cout<<a[i] << " ";
  41.         }
  42.         cout << endl;
  43.     }
  44. }
Add Comment
Please, Sign In to add comment