-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksortproc.adb
More file actions
100 lines (78 loc) · 2.36 KB
/
Copy pathquicksortproc.adb
File metadata and controls
100 lines (78 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
with Ada.Text_IO; use Ada;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Bounded;
with Ada.integer_text_io; use Ada.integer_text_io;
with ada.numerics.elementary_functions; use ada.numerics.elementary_functions;
with Ada.Command_Line; use Ada.Command_Line;
procedure quicksortproc is
array_size : Integer;
arrayPos : Integer;
FileName: String := Ada.Command_Line.Argument(1);
Integer_File : Text_IO.File_Type;
type Node;
type List is access Node;
type Node is record
number: Integer;
next: List;
end record;
type Integer_Array is array(Integer range <>) of integer;
Integer_List: List;
function partition(items: in out Integer_Array; lo: integer; high: integer) return Integer is
pivot: Integer := items(lo);
i: Integer := lo - 1;
j: Integer := high + 1;
temp: Integer := 0;
begin
loop
loop
i := i + 1;
exit when items(i) >= pivot;
end loop;
loop
j := j - 1;
exit when items(j) <= pivot;
end loop;
if (i >= j) then
return j;
end if;
temp := items(i);
items(i) := items(j);
items(j) := temp;
end loop;
end partition;
procedure quicksort(items: in out Integer_Array; lo: Integer; high: Integer) is
p: Integer := 0;
begin
if (lo < high) then
p := partition(items, lo, high);
quicksort(items, lo, p);
quicksort(items, p+1, high);
end if;
end quicksort;
-- main
begin
Text_IO.Open(File=>Integer_File, Name=>FileName, Mode=>Text_IO.In_File);
array_size := 0;
while (not Text_IO.End_Of_File(Integer_File)) loop
array_size := array_size + 1;
Integer_List := new Node'(number=>0, next=>Integer_List);
Integer_Text_IO.Get(Integer_File, Integer_List.number);
end loop;
-- Create my dynamic sized array to hold all numbers for sorting
declare
nums_array : Integer_Array(0..array_size);
begin
-- transfer the numbers from the linked list into the array
arrayPos := 0;
while (Integer_List.next /= null) loop
nums_array(arrayPos) := Integer_List.number;
Integer_List := Integer_List.next;
arrayPos:= arrayPos + 1;
end loop;
quicksort(nums_array, 0, array_size);
-- output sorted values
for i in 0..array_size-1 loop
put_line(Integer'Image(nums_array(i)));
end loop;
end;
end quicksortproc;