fork download
  1. with Ada.Text_IO; use Ada.Text_IO;
  2. with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
  3.  
  4. procedure Test_Sort is
  5.  
  6. type array_type is array (Integer range <>) of Integer;
  7.  
  8. -- constants for choosing sort
  9. small : constant Integer := 5;
  10. medium : constant Integer := 10;
  11.  
  12. -- Insertion Sort
  13. procedure sort_1(A : in out array_type) is
  14. Temp : Integer;
  15. J : Integer;
  16. begin
  17. for I in A'First + 1 .. A'Last loop
  18. Temp := A(I);
  19. J := I - 1;
  20. while J >= A'First and then A(J) > Temp loop
  21. A(J + 1) := A(J);
  22. exit when J = A'First;
  23. J := J - 1;
  24. end loop;
  25. if A(J) > Temp then
  26. A(J + 1) := A(J);
  27. A(J) := Temp;
  28. else
  29. A(J + 1) := Temp;
  30. end if;
  31. end loop;
  32. end sort_1;
  33.  
  34. -- Bubble Sort
  35. procedure sort_2(A : in out array_type) is
  36. Temp : Integer;
  37. Swapped : Boolean := True;
  38. begin
  39. while Swapped loop
  40. Swapped := False;
  41. for I in A'First .. A'Last - 1 loop
  42. if A(I) > A(I + 1) then
  43. Temp := A(I);
  44. A(I) := A(I + 1);
  45. A(I + 1) := Temp;
  46. Swapped := True;
  47. end if;
  48. end loop;
  49. end loop;
  50. end sort_2;
  51.  
  52. -- Heap Sort
  53. procedure sort_3(A : in out array_type) is
  54.  
  55. procedure Heapify(N, I : Integer) is
  56. Largest : Integer := I;
  57. L : Integer := 2 * I + 1;
  58. R : Integer := 2 * I + 2;
  59. Temp : Integer;
  60.  
  61. function To_Index(X : Integer) return Integer is
  62. begin
  63. return A'First + X;
  64. end To_Index;
  65. begin
  66. if L < N and then A(To_Index(L)) > A(To_Index(Largest)) then
  67. Largest := L;
  68. end if;
  69. if R < N and then A(To_Index(R)) > A(To_Index(Largest)) then
  70. Largest := R;
  71. end if;
  72. if Largest /= I then
  73. Temp := A(To_Index(I));
  74. A(To_Index(I)) := A(To_Index(Largest));
  75. A(To_Index(Largest)) := Temp;
  76. Heapify(N, Largest);
  77. end if;
  78. end Heapify;
  79.  
  80. N : constant Integer := A'Length;
  81. Temp : Integer;
  82.  
  83. function To_Index(X : Integer) return Integer is
  84. begin
  85. return A'First + X;
  86. end To_Index;
  87.  
  88. begin
  89. for I in reverse 0 .. (N / 2 - 1) loop
  90. Heapify(N, I);
  91. end loop;
  92.  
  93. for I in reverse 1 .. N - 1 loop
  94. Temp := A(A'First);
  95. A(A'First) := A(To_Index(I));
  96. A(To_Index(I)) := Temp;
  97. Heapify(I, 0);
  98. end loop;
  99. end sort_3;
  100.  
  101. -- General sort procedure
  102. procedure sort(A : in out array_type) is
  103. begin
  104. if A'Length <= small then
  105. sort_1(A);
  106. elsif A'Length <= medium then
  107. sort_2(A);
  108. else
  109. sort_3(A);
  110. end if;
  111. end sort;
  112.  
  113. -- Test arrays
  114. A : array_type(1 .. 10) := (10, -5, 3, 0, 8, -2, 7, 4, 1, 6);
  115. B : array_type(1 .. 5) := (3, 1, 4, 5, 2);
  116.  
  117. begin
  118. Put_Line("Before sorting A:");
  119. for I in A'Range loop Put(A(I)'Img & " "); end loop;
  120. New_Line;
  121.  
  122. Put_Line("Before sorting B:");
  123. for I in B'Range loop Put(B(I)'Img & " "); end loop;
  124. New_Line;
  125.  
  126. sort(A);
  127. sort(B);
  128.  
  129. Put_Line("After sorting A:");
  130. for I in A'Range loop Put(A(I)'Img & " "); end loop;
  131. New_Line;
  132.  
  133. Put_Line("After sorting B:");
  134. for I in B'Range loop Put(B(I)'Img & " "); end loop;
  135. New_Line;
  136.  
  137. end Test_Sort;
  138.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Before sorting A:
 10 -5  3  0  8 -2  7  4  1  6 
Before sorting B:
 3  1  4  5  2 
After sorting A:
-5 -2  0  1  3  4  6  7  8  10 
After sorting B:
 1  2  3  4  5