Advertisement
SergeyPGUTI

9.7(математика на помощ)

Mar 26th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. using namespace std;
  6. int a[100];
  7.  
  8. void quickSort(int l, int r){
  9.     srand (time(NULL));
  10.     //int x=rand() % (r-l);
  11.    // x=a[x];
  12.    int x = a[l + (r - l) / 2]; //плохой вариант пивота
  13.     //запись эквивалентна (l+r)/2,
  14.     //но не вызввает переполнения на больших данных
  15.     int i = l;
  16.     int j = r;
  17.     //код в while обычно выносят в процедуру
  18.     while(i <= j)
  19.     {
  20.         while(a[i] < x) i++;
  21.         while(a[j] > x) j--;
  22.         if(i <= j)
  23.         {
  24.             swap(a[i], a[j]);
  25.             i++;
  26.             j--;
  27.         }
  28.     }
  29.     if (i<r)
  30.         quickSort(i, r);
  31.  
  32.     if (l<j)
  33.         quickSort(l, j);
  34. }
  35.  
  36. int main()
  37. {
  38.     string s;
  39.     cin>>s;
  40.     int n=0;
  41.     for (int i=0;i<s.length();i+=2)
  42.     {
  43.        a[n]=s[i]-'0';
  44.        n++;
  45.     }
  46.     quickSort(0,n-1);
  47.     for (int i=0;i<n;i++)
  48.     {
  49.         cout<<a[i];
  50.         if (i!=n-1) cout<<"+";
  51.     }
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement