-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqsort_exam.adb
131 lines (121 loc) · 3.65 KB
/
qsort_exam.adb
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
-- =======================================================
-- Ada WCET Benchmark v.1.0
-- University of Padua
-- =======================================================
-- Porting to Ada of C WCET becnhmark from:
--
-- Mälardalen WCET research group:
-- http://www.mrtc.mdh.se/projects/wcet/benchmarks.html
--
-- SNU-RT Benchmark Suite for Worst Case Timing Analysis
-- Real-Time Research Group, Seoul National University
-- S.-S. Lim ([email protected])
-- =======================================================
with Interfaces.C; use Interfaces.C;
with Interfaces; use Interfaces;
-- with System.IO; use System.IO;
package body Qsort_exam is
Istack : Stack_Array := (others => 0);
-- procedure Print_Array is
-- begin
-- Put("Array: ");
-- for I in Arr'Range loop
-- Put_Line(" " & Float'Image(Arr(I)));
-- -- Put_Line(" " & Float'Image(Arr2(I)) & " " & Float'Image(Arr(I)));
-- end loop;
-- end Print_Array;
--
-- procedure Print_Part (start_ind:Integer; end_ind:Integer)is
-- begin
-- for I in start_ind..end_ind loop
-- Put_Line(" " & Float'Image(Arr(I)));
-- end loop;
-- end Print_Part;
procedure Swap (i : Integer; j : Integer) is
Temp : Float;
begin
Temp := Arr (i);
Arr (i) := Arr (j);
Arr (j) := Temp;
end Swap;
function Sort (n : Integer) return Integer is
i, ir, j, k, l : Integer;
jstack : Integer;
a : Float;
begin
ir := n;
l := 1;
jstack := 0;
while True loop
if (ir - l < 7) then
-- Simple Insertion Sort
for j in (l + 1) .. ir loop
a := Arr (j);
i := j - 1;
while i >= l and then Arr (i) >a loop
Arr (i + 1) := Arr (i);
i := i - 1;
end loop;
Arr (i + 1) := a;
end loop;
if (jstack = 0) then
-- Print_Array;
return 0;
end if;
ir := Istack (jstack);
jstack := jstack - 1;
l := Istack (jstack);
jstack := jstack - 1;
else
-- Non recursive Quick Sort
-- Divide step
-- Right_Shift (Integer'size on LEON is 32)
k := Integer (Shift_Right (Unsigned_32 (l +ir),1));
Swap (k, l + 1);
if (Arr (l)) > Arr (ir) then
Swap (l, ir);
end if;
if (Arr (l + 1) > Arr (ir)) then
Swap (l + 1, ir);
end if;
if (Arr (l) > Arr (l+1)) then
Swap (l, l + 1);
end if;
i := l + 1;
j := ir;
a := Arr (l + 1);
while True loop
loop
i := i + 1;
exit when (Arr (i) >= a);
end loop;
loop
j := j - 1;
exit when (Arr (j) <= a);
end loop;
exit when (j < i);
Swap (i, j);
end loop;
Swap (l + 1, j);
jstack := jstack + 2;
if (ir - i + 1 >= j-l) then
Istack (jstack) := ir;
Istack (jstack - 1) := i;
ir := j - 1;
else
Istack (jstack) := j - 1;
Istack (jstack - 1) := l;
l := i;
end if;
end if;
end loop;
return 2;
end Sort;
procedure Qsort_main is
res : Integer;
begin
Arr := Init_Arr;
res := Sort (20);
end Qsort_main;
end Qsort_exam;