Selection Sort

Sommaire du document

Description du code

Exemple de tri par sélection

Code source ou contenu du fichier


Code Pascal (Selection Sort) (66 lignes) :
  1. program selectionSort;
  2. function maxValueIndex(var a : array of integer; minIndex, maxIndex : integer);
  3. {Pre: a defined
  4. maxIndex>=minIndex;
  5. minIndex>=0;
  6. a[minIndex..maxIndex] defined
  7.  Post: for each i into minIndex..maxIndex, a[maxIndex] >= a[i]
  8. }
  9. var tempMax : integer;
  10. begin
  11. if minIndex >= maxIndex then maxValueIndex := minIndex;
  12. else begin
  13. tempMax := maxValueIndex(a, minIndex+1, maxIndex);
  14. if a[minIndex]<a[tempMax]
  15. then maxValueIndex := tempMax;
  16. else maxValueIndex := minIndex;
  17. end
  18. end;
  19. procedure swap(var a : array of integer; i, j : integer);
  20. {Pre: a defined
  21. j>i;
  22. i>=0;
  23. a[i] defined
  24. a[j] defined
  25.  Post: a[i]=old value at a[j] et a[j]=old value at a[i]
  26. }
  27. var temp: integer;
  28. begin
  29. temp := a[i];
  30. a[i] := a[j];
  31. a[j] := temp;
  32. end;
  33. procedure selectionSort(var a : array of integer; arraySize : integer);
  34. {Pre: a defined
  35. arraySize>=0;
  36. a indexed from 0 to arraySize -1
  37. for each i into 0..arraySize-1, a[i] defined
  38.  Post: for each i into 0..arraySize-2, a[i] <= a[i+1]
  39. }
  40. var i : integer;
  41. begin
  42. for i := arraySize downto 0 do
  43. swap(a, i, maxValueIndex(a,1,i));
  44. end;
  45. {main program
  46. }
  47. begin
  48. const maxIndice = 7;
  49. var testArray : packed array[0..maxIndice] of integer;
  50. testArray[0] := 8;
  51. testArray[1] := 4;
  52. testArray[2] := 6;
  53. testArray[3] := 2;
  54. testArray[4] := 13;
  55. testArray[5] := 4;
  56. testArray[6] := 1;
  57. testArray[7] := 20;
  58. var i : integer;
  59. writeln('Unsorted array :');
  60. for i:=0 to maxIndice do
  61. writeln('value ', testArray[i], ' at index ', i);
  62. selectionSort(testArray, maxIndice+1);
  63. writeln('Sorted array :');
  64. for i:=0 to maxIndice do
  65. writeln('value ', testArray[i], ' at index ', i);
  66. end.

Autres extraites de codes en Pascal

Merge Sort Exemple de tri par fusion
Selection Sort Exemple de tri par sélection
Suite de Fibonacci Exemple de récursion en Pascal
Suite de Fibonacci Exemple de méoïsation en Pascal
Tous les extraits

 

Réseaux sociaux

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.

 

Nuage de mots clés

9 mots clés dont 5 définis manuellement (plus d'information...).

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher le nuage de mots clés.

 

Astuce pour imprimer les couleurs des cellules de tableaux : http://www.gaudry.be/ast-rf-450.html
Aucun commentaire pour cette page

© Ce document issu de l′infobrol est enregistré sous le certificat Cyber PrInterDeposit Digital Numbertection. Enregistrement IDDN n° 5329-308
Document créé le 05/10/09 01:03, dernière modification le Vendredi 17 Juin 2011, 12:12
Source du document imprimé : http://www.gaudry.be/sniplet.html Document affiché 66 fois ce mois de Juin.
St.Gaudry©07.01.02
Outils (masquer)
||
Recherche (afficher)
Recherche :

Utilisateur (masquer)
Apparence (afficher)
Stats (afficher)
15832 documents
452 astuces.
549 niouzes.
3099 definitions.
447 membres.
8115 messages.

Document genere en :
0,36 seconde

Mises à jour :
Mises à jour du site
Citation (masquer)
Rien n'est impossible, seules les limites de nos esprits définissent certaines choses comme inconcevables.

Marc Levy [Extrait de Et si c'était vrai...]
 
l'infobrol
Nous sommes le Dimanche 03 Juin 2012, 11:42, toutes les heures sont au format GMT+1.00 Heure, heure d'été (+1)