Advertisement
Guest User

heap

a guest
Dec 10th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1.         struct Ucenik
  2.         {
  3.             public string ime;
  4.             public int usmeno, pismeno, ukupno;
  5.         }
  6.         void napraviHeap(Ucenik[] a, int n)
  7.         {
  8.             for (int i = 1; i < n; i++)
  9.             {
  10.                 int k = i;
  11.                 while ((k - 1) / 2 >= 0 && a[k].ukupno > a[(k - 1) / 2].ukupno)
  12.                 //od manj kad vec while ((k - 1) / 2 >= 0 && a[k].ukupno > a[(k - 1) / 2].ukupno)
  13.                     {
  14.                     Ucenik pom = a[k];
  15.                     a[k] = a[(k - 1) / 2];
  16.                     a[(k - 1) / 2] = pom;
  17.                     k = (k - 1) / 2;
  18.                 }
  19.             }
  20.         }
  21.         void heapSort(Ucenik[] a, int n)
  22.         {
  23.             for (int i = n - 1; i >= 0; i--)
  24.             {
  25.                 Ucenik pom = a[i];
  26.                 a[i] = a[0];
  27.                 a[0] = pom;
  28.  
  29.                 int l, d, max;
  30.                 int k = 0;
  31.                 bool ok = false;
  32.                 while (!ok)
  33.                 {
  34.                     l = 2 * k + 1;
  35.                     d = 2 * k + 2;
  36.                     max = k;
  37.                     if (l < i && a[l].ukupno > a[max].ukupno) max = l;
  38.                     if (d < i && a[d].ukupno > a[max].ukupno) max = d;
  39.                     //od manjeg ka vecem if (l < i && a[l].ukupno > a[max].ukupno) max = l;
  40.                     //od manjeg ka vecemif (d < i && a[d].ukupno > a[max].ukupno) max = d;
  41.                     if (max != k)
  42.                     {
  43.                         pom = a[max];
  44.                         a[max] = a[k];
  45.                         a[k] = pom;
  46.                         k = max;
  47.                     }
  48.                     else ok = true;
  49.                 }
  50.             }
  51.         }
  52.         int n;
  53.         Ucenik[] u = new Ucenik[20];
  54.  
  55.  
  56.  
  57.             napraviHeap(u, n);
  58.             heapSort(u, n);
  59.             for (int i = n - 1; i >= 0; i--)
  60.             {
  61.                 listBox2.Items.Add(u[i].ime + " " + u[i].usmeno + "," + u[i].pismeno);
  62.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement