Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

26 lignes
544B

  1. template <class C>
  2. inline void reheap (C& a, int n, int i) {
  3. while (2*i+1<n)
  4. {
  5. int l = 2*i+1;
  6. int r = l+1;
  7. int k = ((r<n) && (a[r]>a[l])) ? r : l;
  8. if (a[k]<=a[i]) break;
  9. std::swap(a[k], a[i]);
  10. i = k;
  11. }
  12. }
  13. template <class C>
  14. void heapsort (C& a)
  15. {
  16. // build the heap by reheaping from the rear
  17. for (int i=a.size()-1; i>=0; i--)
  18. reheap(a, a.size(), i);
  19. // build the sorted list by popping the heap
  20. for (int i=a.size()-1; i>=0; i--) {
  21. std::swap(a[0],a[i]);
  22. reheap(a, i, 0);
  23. }
  24. }