Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. // Sortowanie metodą Shella
  2. //--------------------------------------------------------
  3. // (C)2012 mgr Jerzy Wałaszek
  4. // I Liceum Ogólnokształcące
  5. // im. K. Brodzińskiego
  6. // w Tarnowie
  7. //--------------------------------------------------------
  8.  
  9. #include <cmath>
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <cstdlib>
  13. #include <time.h>
  14.  
  15. using namespace std;
  16.  
  17. const int N = 20; // Liczebność zbioru.
  18.  
  19. // Program główny
  20. //---------------
  21.  
  22. int main()
  23. {
  24.   int d[N],h,i,j,x;
  25.  
  26.   cout << " Sortowanie metoda Shella\n"
  27.           "--------------------------\n"
  28.           "  (C)2005 Jerzy Walaszek\n\n"
  29.           "Przed sortowaniem:\n\n";
  30.  
  31. // Najpierw wypełniamy tablicę d[] liczbami pseudolosowymi
  32. // a następnie wyświetlamy jej zawartość
  33.  
  34.   srand((unsigned)time(NULL));
  35.  
  36.   for(i = 0; i < N; i++) d[i] = rand() % 100;
  37.   for(i = 0; i < N; i++) cout << setw(4) << d[i];
  38.   cout << endl;
  39.  
  40. // Wyznaczamy wartość początkowego przesunięcia
  41.  
  42.   for(h = 1; h < N; h = 3 * h + 1);
  43.   h /= 9;
  44.   if(!h) h++; // istotne dla małych N, dla większych można pominąć!
  45.  
  46. // Sortujemy
  47.  
  48.   while(h)
  49.   {
  50.     for(j = N - h - 1; j >= 0; j--)
  51.     {
  52.       x = d[j];
  53.       i = j + h;
  54.       while((i < N) && (x > d[i]))
  55.       {
  56.         d[i - h] = d[i];
  57.         i += h;
  58.       }
  59.       d[i - h] = x;
  60.     }
  61.     h /= 3;
  62.   }
  63.  
  64. // Wyświetlamy wynik sortowania
  65.  
  66.   cout << "Po sortowaniu:\n\n";
  67.   for(i = 0; i < N; i++) cout << setw(4) << d[i];
  68.   cout << endl;
  69.   return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement