Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. // Пирамидальная сортировка (по возрастанию)
  2. // Шамовцев, 2011
  3. // реализация -  стыдно :)
  4. #include <iostream>
  5. #include <conio.h>
  6. #include <time.h>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. void iswap(int &n1, int &n2)
  12. {
  13.     int temp = n1;
  14.     n1 = n2;
  15.     n2 = temp;
  16. }
  17.  
  18. int getrandom(int &n)
  19. {
  20.     int random = 0;
  21.  
  22.  
  23.     random = rand()%n;
  24.  
  25.     return random ;
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32. int main()
  33. {
  34.     srand ( time(NULL) );
  35.  
  36.  
  37. st:
  38.     int  k,N = 0;
  39.     cout << "Please input size"  ;
  40.     cin >> N;
  41.     int a[N];
  42.  
  43.     cout << "Lets generate our mass with size " <<N <<". Press Enter to continue" << endl;
  44.     k = getch();
  45.     if (k==13)
  46.  
  47.  
  48.  
  49.         for ( int i = 0; i < N; ++i )
  50.         {
  51.             a[i] = getrandom(N);
  52.  
  53.             cout << a[i] << " ";
  54.         }
  55.  
  56.  
  57.     clock_t t0 = clock();
  58.  
  59.  
  60.  
  61.     int sh = 0; //ñìåùåíèå
  62.     bool b = false;
  63.     for(;;)
  64.     {
  65.         b = false;
  66.         for ( int i = 0; i < N; i++ )
  67.         {
  68.             if( i * 2 + 2 + sh < N )
  69.             {
  70.                 if( ( a[i + sh] > /*<*/ a[i * 2 + 1 + sh] ) || ( a[i + sh] > /*<*/ a[i * 2 + 2 + sh] ) )
  71.                 {
  72.                     if ( a[i * 2 + 1 + sh] < /*>*/ a[i * 2 + 2 + sh] )
  73.                     {
  74.                         iswap( a[i + sh], a[i * 2 + 1 + sh] );
  75.                         b = true;
  76.                     }
  77.                     else if ( a[i * 2 + 2 + sh] < /*>*/ a[ i * 2 + 1 + sh])
  78.                     {
  79.                         iswap( a[ i + sh], a[i * 2 + 2 + sh]);
  80.                         b = true;
  81.                     }
  82.                 }
  83.             }
  84.             else if( i * 2 + 1 + sh < N )
  85.             {
  86.                 if( a[i + sh] > /*<*/ a[ i * 2 + 1 + sh] )
  87.                 {
  88.                     iswap( a[i + sh], a[i * 2 + 1 + sh] );
  89.                     b = true;
  90.                 }
  91.             }
  92.         }
  93.         if (!b) sh++;
  94.         if ( sh + 2 == N ) break;
  95.     }  
  96.  
  97.     cout << endl << endl;
  98.  
  99.  
  100.     clock_t t1 = clock();
  101.  
  102.     cout << "Lets watch our SORTED mass. Press Enter to continue" << endl;
  103.     k = getch();
  104.     if (k==13)
  105.     {
  106.  
  107.  
  108.         for ( int i = 0; i < N; ++i ) cout << a[i] << " ";
  109.     }
  110.  
  111.  
  112.  
  113.  
  114.     cout <<endl <<  "Elapsed time: " << (double)(t1 - t0) / CLOCKS_PER_SEC <<" ms"<< endl
  115.          << "again? Press Enter, or any key to quit" << endl;
  116.     k = getch();
  117.     if (k==13)
  118.     {
  119.         goto st;
  120.     }
  121.     else
  122.  
  123.         _getch();
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement