Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Random sort
- --
- with Ada.Numerics.Discrete_Random;
- with Ada.Text_IO;
- procedure Sort is
- package Discrete_Random is
- new Ada.Numerics.Discrete_Random (Result_Subtype => Natural);
- Sequence : array (1 .. 6) of Natural := (23, 42, 4, 16, 15, 8);
- Is_Sorted : Boolean := False;
- Gen : Discrete_Random.Generator;
- begin
- while not Is_Sorted loop
- Is_Sorted := True;
- -- Random Knuth shuffle of Sequence
- Discrete_Random.Reset (Gen);
- for I in Sequence'Range loop
- declare
- J : constant Positive := (Discrete_Random.Random (Gen) mod I) + 1;
- Tmp : constant Natural := Sequence (I);
- begin
- Sequence (I) := Sequence (J);
- Sequence (J) := Tmp;
- end;
- end loop;
- -- Check if Sequence is sorted
- for I in Sequence'First .. Sequence'Last - 1 loop
- if Sequence (I) > Sequence (I + 1) then
- Is_Sorted := False;
- exit;
- end if;
- end loop;
- end loop;
- -- Print sorted Sequence
- for I in Sequence'Range loop
- Ada.Text_IO.Put_Line (Sequence (I)'Image);
- end loop;
- end Sort;
Advertisement
Comments
-
- https://2captcha.com/?from=18906076
-
- --
- -- Random sort
- --
- with Ada.Numerics.Discrete_Random;
- with Ada.Text_IO;
- procedure Sort is
- package Discrete_Random is
- new Ada.Numerics.Discrete_Random (Result_Subtype => Natural);
- Sequence : array (1 .. 6) of Natural := (23, 42, 4, 16, 15, 8);
- Is_Sorted : Boolean := False;
- Gen : Discrete_Random.Generator;
- begin
- while not Is_Sorted loop
- Is_Sorted := True;
- -- Random Knuth shuffle of Sequence
- Discrete_Random.Reset (Gen);
- for I in Sequence'Range loop
- declare
- J : constant Positive := (Discrete_Random.Random (Gen) mod I) + 1;
- Tmp : constant Natural := Sequence (I);
- begin
- Sequence (I) := Sequence (J);
- Sequence (J) := Tmp;
- end;
- end loop;
- -- Check if Sequence is sorted
- for I in Sequence'First .. Sequence'Last - 1 loop
- if Sequence (I) > Sequence (I + 1) then
- Is_Sorted := False;
- exit;
- end if;
- end loop;
- end loop;
- -- Print sorted Sequence
- for I in Sequence'Range loop
- Ada.Text_IO.Put_Line (Sequence (I)'Image);
- end loop;
- end Sort;
Add Comment
Please, Sign In to add comment
Advertisement