Kód: Vybrať všetko
PROGRAM CompareSorts;
USES
Crt,Dos;
CONST
Count = 1000;
Count1 = 15000;
Count2 = 32000;
limit = 255;
TYPE
Raytype = array[1..count] of integer;
Raytype1 = array[1..count1] of integer;
Raytype2 = array[1..count2] of integer;
Procedure FillArray(VAR List:raytype);
VAR
X : integer;
Begin
For X := 1 to high(raytype) Do Begin
List[X] := Random(limit) + 1;
End;
End;
Procedure ClearArray(VAR List:Raytype);
VAR
X : integer;
Begin
For X := 0 to high(raytype) Do Begin
List[X] := 0;
End;
End;
Function Seconds : real;
VAR
Hr,Min,Sec,Hund : word;
Begin
GetTime(Hr,Min,Sec,Hund);
Seconds := Hr*3600 + Min*60 + Sec + Hund/100;
End;
Procedure DisplayTime(VAR Finish:real; Start:real; Adj:real);
VAR
Day,Time : real;
Min,Sec : integer;
Begin
Finish := Seconds;
Day := 24*3600;
Time := Finish - Start - Adj;
If Time < 0 then
Time := Time + Day;
Min := Trunc(Time) Div 60;
Write(Min:3,':');
If Time-Min*60 < 10 then
Write('0',Time-Min*60:4:2)
Else
Write(Time-Min*60:5:2);
End;
Procedure BubbleSort(VAR List:RayType; N:integer);
VAR
X,Y,Temp : integer;
Begin
For X := 1 to N - 1 Do Begin
For Y := X + 1 to N Do Begin
If List[X] > List[Y] then Begin
Temp := List[X];
List[X] := List[Y];
List[Y] := Temp;
End;
End;
End;
End;
Procedure QuickSort(VAR A:raytype; L,R : integer);
VAR
i,j,PIV,t : integer;
Begin
If L < R then Begin
i := L + 1;
j := R;
PIV := A[L];
Repeat
While A[i] <= PIV Do
i := i + 1;
While A[j] > PIV Do
j := j - 1;
If i < j then Begin
t := A[i];
A[i] := A[j];
A[j] := t;
End;
Until i > j;
A[L] := A[j];
A[j] := PIV;
Quicksort(A,L,j - 1);
Quicksort(A,i,R)
End;
End;
Procedure SortArray(VAR TestList,OriginList:raytype; column:integer);
VAR
Start,Adj,Finish : real;
Begin
ClearArray(TestList);
FillArray(TestList);
OriginList := TestList;
GotoXY(24,24);
Adj := 0;
Write('Adjustment = ');
Start := Seconds;
DisplayTime(Finish,Start,Adj);
Adj := Finish - Start;
GotoXY(1,column); Write('BubbleSort');
GotoXY(1,column+1);
Start := Seconds;
BubbleSort(TestList,Count);
DisplayTime(Finish,Start,Adj);
Write(^G);
TestList := OriginList;
GotoXY(30,column); Write('QuickSort');
GotoXY(30,column+1);
Start := Seconds;
QuickSort(TestList,1,Count);
DisplayTime(Finish,Start,Adj);
Write(^G);
End;
Procedure Empty(VAR List:raytype);
Begin
End;
VAR
TestList,OriginList : raytype;
TestList1,OriginList1 : raytype;
TestList2,OriginList2 : raytype;
X : integer;
Begin
Randomize;
ClrScr;
Writeln(' 23. Run various sorts with a common array to compare the',
' completion time.');
For X := 1 to 80 Do Write(chr(196));
Writeln; Writeln;
For X := 1 to 80 Do Write(chr(196));
GotoXY(1,23);
For X := 1 to 80 Do Write(chr(196));
SortArray (TestList, OriginList, 3);
SortArray (TestList1, OriginList1, 6);
SortArray (TestList2, OriginList2, 10);
Write('- press any key to continue -':48);
End.