Todvard
Jul 25th, 2023
125
0
Never
This is comment for paste Random sort
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --
  2. -- Random sort
  3. --
  4. with Ada.Numerics.Discrete_Random;
  5. with Ada.Text_IO;
  6.  
  7.  
  8. procedure Sort is
  9. package Discrete_Random is
  10. new Ada.Numerics.Discrete_Random (Result_Subtype => Natural);
  11.  
  12. Sequence : array (1 .. 6) of Natural := (23, 42, 4, 16, 15, 8);
  13. Is_Sorted : Boolean := False;
  14. Gen : Discrete_Random.Generator;
  15. begin
  16. while not Is_Sorted loop
  17. Is_Sorted := True;
  18.  
  19. -- Random Knuth shuffle of Sequence
  20. Discrete_Random.Reset (Gen);
  21. for I in Sequence'Range loop
  22. declare
  23. J : constant Positive := (Discrete_Random.Random (Gen) mod I) + 1;
  24. Tmp : constant Natural := Sequence (I);
  25. begin
  26. Sequence (I) := Sequence (J);
  27. Sequence (J) := Tmp;
  28. end;
  29. end loop;
  30.  
  31. -- Check if Sequence is sorted
  32. for I in Sequence'First .. Sequence'Last - 1 loop
  33. if Sequence (I) > Sequence (I + 1) then
  34. Is_Sorted := False;
  35.  
  36. exit;
  37. end if;
  38. end loop;
  39. end loop;
  40.  
  41. -- Print sorted Sequence
  42. for I in Sequence'Range loop
  43. Ada.Text_IO.Put_Line (Sequence (I)'Image);
  44. end loop;
  45. end Sort;
  46.  
Add Comment
Please, Sign In to add comment