Merge Sort

Description du code

Exemple de tri par fusion

Code source ou contenu du fichier

  1. function mergeSort(var a : array of integer; minIndex, maxIndex : integer);
  2. {Pre: a defined
  3. maxIndex>=minIndex;
  4. minIndex>=0;
  5. a[minIndex..maxIndex] defined
  6.  Post: for each i into minIndex..maxIndex, a[maxIndex] >= a[i]
  7. }
  8. var center : integer; {center of the array}
  9. begin
  10. if minIndex > maxIndex {O(1)}
  11. then begin
  12. center := (minIndex + maxIndex) div 2; {O(1)}
  13. mergeSort(a, minIndex, center); {T(n/2)}
  14. mergeSort(a, center+1, maxIndex); {T(n/2)}
  15. merge(a, minIndex, center, maxIndex); {O(n)}
  16. end
  17. end;

Autres extraits de codes en Pascal

Document créé le 05/10/2009, dernière modification le 28/10/2018
Source du document imprimé : https://www.gaudry.be/sniplet-rf-pascal/tri-fusion.pas.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.