Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int n;
  7. int rnp; // ilosc nieposortowanych
  8.  
  9. void wyswietl(int t[], int r, int r2)
  10. {
  11.     for (int i = 0; i < r2; i++)
  12.         cout << t[i] << " ";
  13.     if (r2 < r) cout << "| ";
  14.     for (int i = r2; i < r; i++)
  15.         cout << t[i] << " ";
  16.     cout << endl;
  17. }
  18. ///////////////////////////////////
  19.  
  20. void przywracanie_kopca(int t[], int k, int r)
  21. {
  22.     int j; int wtemp = t[k - 1];
  23.     while (k <= r / 2)
  24.     {
  25.         j = 2 * k;
  26.         if (j<r && t[j - 1]<t[j]) j++;
  27.         if (wtemp >= t[j - 1]) break;
  28.         else
  29.         {
  30.             t[k - 1] = t[j - 1];
  31.             k = j;
  32.         }
  33.     }
  34.     t[k - 1] = wtemp;
  35.     wyswietl(t, n, rnp);
  36. }
  37. ////////////////////////////////////////////////////////////////////
  38. void sort_kopcowanie(int t[], int r)
  39. {
  40.     int wtemp;
  41.     for (int k = r / 2;k>0;k--) przywracanie_kopca(t, k, r);
  42.     cout << "odczepianie najwieszych elementow..." << endl;
  43.     cout << "i przywracanie wlasnosci kopca dla pozostalych..." << endl;
  44.     do
  45.     {
  46.         wtemp = t[0];
  47.         rnp = r;
  48.         t[0] = t[r - 1]; t[r - 1] = wtemp;
  49.         r--;
  50.         przywracanie_kopca(t, 1, r);
  51.          
  52.     } while (r>1);
  53. }
  54. //////////////////////////////////////////////////////////
  55.  
  56.  
  57. /////////////////////////////////////////////////////////////////
  58.  
  59.  
  60. int main()
  61. {
  62.     int tab1[] = { 44,32,74,51,46,71,49,73,86,34 };
  63.     int tab2[] = { 1,2,3,4,5,6,7,8,9,10 };
  64.     int tab3[] = { 10,9,8,7,6,5,4,3,2,1 };
  65.  
  66.     sort_kopcowanie(tab1, 10);
  67.     system("pause");
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement