Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Ucenik
- {
- public string ime;
- public int usmeno, pismeno, ukupno;
- }
- void napraviHeap(Ucenik[] a, int n)
- {
- for (int i = 1; i < n; i++)
- {
- int k = i;
- while ((k - 1) / 2 >= 0 && a[k].ukupno > a[(k - 1) / 2].ukupno)
- //od manj kad vec while ((k - 1) / 2 >= 0 && a[k].ukupno > a[(k - 1) / 2].ukupno)
- {
- Ucenik pom = a[k];
- a[k] = a[(k - 1) / 2];
- a[(k - 1) / 2] = pom;
- k = (k - 1) / 2;
- }
- }
- }
- void heapSort(Ucenik[] a, int n)
- {
- for (int i = n - 1; i >= 0; i--)
- {
- Ucenik pom = a[i];
- a[i] = a[0];
- a[0] = pom;
- int l, d, max;
- int k = 0;
- bool ok = false;
- while (!ok)
- {
- l = 2 * k + 1;
- d = 2 * k + 2;
- max = k;
- if (l < i && a[l].ukupno > a[max].ukupno) max = l;
- if (d < i && a[d].ukupno > a[max].ukupno) max = d;
- //od manjeg ka vecem if (l < i && a[l].ukupno > a[max].ukupno) max = l;
- //od manjeg ka vecemif (d < i && a[d].ukupno > a[max].ukupno) max = d;
- if (max != k)
- {
- pom = a[max];
- a[max] = a[k];
- a[k] = pom;
- k = max;
- }
- else ok = true;
- }
- }
- }
- int n;
- Ucenik[] u = new Ucenik[20];
- napraviHeap(u, n);
- heapSort(u, n);
- for (int i = n - 1; i >= 0; i--)
- {
- listBox2.Items.Add(u[i].ime + " " + u[i].usmeno + "," + u[i].pismeno);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement