Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. int tab[10];
  7. int t[10];
  8.  
  9.  
  10. void scalaj(int start, int mid, int _end)
  11. {
  12.     int i,j,q;
  13.     for (i = start; i <= _end; i++)
  14.         t[i]=tab[i];
  15.     i = start; j = mid+1; q = start;
  16.     while (i <= mid && j <= _end)
  17.     {
  18.         if (t[i] < t[j])
  19.             tab[q++] = t[i++];
  20.         else
  21.             tab[q++] = t[j++];
  22.     }
  23.     while (i <= mid)
  24.         tab[q++] = t[i++];
  25. }
  26.  
  27.  
  28. void sortuj(int start, int _end)
  29. {
  30.     int mid;
  31.     if (start<_end)
  32.     {
  33.         mid = (start+_end)/2;
  34.         sortuj(start, mid);
  35.         sortuj(mid+1, _end);
  36.         scalaj(start, mid, _end);
  37.     }
  38. }
  39.  
  40.  
  41. int main()
  42. {
  43.     srand( time( NULL ) );
  44.     cout << "Liczby przed posortowaniem" << endl;
  45.  
  46.     for(int i = 0; i < 10; i++)
  47.     {
  48.         tab[i] = (rand() % 10) + 1;
  49.         cout << tab[i] << " ";
  50.     }
  51.  
  52.     sortuj(0,9);
  53.     cout << endl;
  54.     cout << "Liczby po posortowaniu" << endl;
  55.  
  56.     for(int i = 0; i < 10; i++)
  57.     {
  58.         cout << tab[i] << " ";
  59.     }
  60.  
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement