unit sorting interface const 10 type index item record key integer end

  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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
unit sorting;
interface
const n=10;
type index = 0..n;
item = record
key:integer;
end;
mas = array[0..n] of item;
var a:mas;
t:byte;
procedure straightInsertion(var a:mas);
procedure straightSelection(var a:mas);
procedure bubleSort(var a:mas);
procedure shakerSort(var a:mas);
procedure quickSort(var a:mas; lo,hi:integer);
implementation
{------------------------------------------------------------}
procedure straightInsertion(var a:mas);
var i,j,count:integer;
x:item;
begin
count:=0;
for i:=2 to n do
begin
x:=a[i];
j:=i-1;
while (j>=1) and (x.key < a[j].key) do
begin
a[j+1]:=a[j];
j:=j-1;
end;
a[j+1]:=x;
inc(count);
end;
writeln('Count: ',count);
end;
{------------------------------------------------------------}
procedure straightSelection(var a:mas);
var i,j,min:index;
x:item;
count:integer;
begin
count:=0;
for i:=1 to n-1 do
begin
min:=i;
x:=a[min];
for j:=i+1 to n do
if a[j].key < x.key then
begin
min:=j;
x:=a[j];
end;
a[min]:=a[i];
a[i]:=x;
inc(count);
end;
writeln('Count: ',count);
end;
{------------------------------------------------------------}
procedure bubleSort(var a:mas);
var i,j:index;
x:item;
count:integer;
begin
count:=0;
for i:=2 to n do
for j:=n downto i do
if a[j-1].key > a[j].key then
begin
x:=a[j-1];
a[j-1]:=a[j];
a[j]:=x;
inc(count);
end;
writeln('Count: ',count);
end;
{------------------------------------------------------------}
procedure shakerSort(var a:mas);
var k,j,left,right:index;
x:item;
count:integer;
begin
left:=2;
right:=n;
k:=n;
count:=0;
repeat
{right to left}
for j:=right downto left do
if a[j-1].key > a[j].key then
begin
x:=a[j-1];
a[j-1]:=a[j];
a[j]:=x;
k:=j;
inc(count);
end;
{write('right to left: ');
printArray(a); }
left:=k+1;
{left ot right}
for j:=left to right do
if a[j-1].key > a[j].key then
begin
x:=a[j-1];
a[j-1]:=a[j];
a[j]:=x;
k:=j;
inc(count);
end;
{write('left to right: ');
printArray(a); }
right:=k-1;
until left > right;
writeln('Count: ',count);
end;
{------------------------------------------------------------}
procedure quickSort(var a:mas; lo,hi:integer);
var count:integer;
procedure sort(left,right:index);
var i,j:index;
x,y:item;
{count:integer;}
begin
i:=left;
j:=right;
x:=a[(left+right) div 2];
repeat
while (a[i].key < x.key) do inc(i);
while (a[j].key > x.key) do dec(j);
if (i<=j) then
begin
if a[i].key > a[j].key then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y;
end;
inc(i);
dec(j);
inc(count);
end;
until i>j;
if left < j then sort(left,j);
if i < right then sort(i,right);
end;
begin
count:=0;
sort(lo,hi);
writeln('Count: ',count);
end;
begin
end.