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
| program Project36;
{$IF RTLVersion >= 13.0}
{$APPTYPE CONSOLE}
uses Windows, SysUtils;
{$ELSE}
uses WinCrt;
function GetTickCount: LongWord; stdcall; external 'kernel32.dll';
{$IFEND}
const
MinLength = 10;
MaxLength = 100;
MinRandom = 0;
MaxRandom = 100;
type
TIntArray = array of integer;
procedure IntArrayAdjustLength(var vLength: integer; var vIntArray: TIntArray);
var
i: integer;
begin
repeat
write('Taille du tableau = ');
readLn(vLength);
until (vLength >= MinLength) and (vLength <= MaxLength);
setLength(vIntArray, vLength);
for i := 0 to vLength-1 do
vIntArray[i] := MinRandom+random(MaxRandom-MinRandom+1);
end;
procedure IntArrayWrite(aIntArray: TIntArray);
var
i, vHi:integer;
begin
vHi := high(aIntArray);
write('[');
for i := 0 to vHi do
begin
write(aIntArray[i]:2);
if i < vHi then
write(', ');
end;
writeLn(']');
end;
procedure IntArrayQuickSort(var vIntArray: TIntArray; iLo, iHi: Integer);
var
vLo, vHi, vPivot, vT : Integer;
begin
vLo := iLo;
vHi := iHi;
vPivot := vIntArray[(vLo + vHi) shr 1];
repeat
while vIntArray[vLo] < vPivot do
Inc(vLo);
while vIntArray[vHi] > vPivot do
Dec(vHi);
if vLo <= vHi then
begin
vT := vIntArray[vLo];
vIntArray[vLo] := vIntArray[vHi];
vIntArray[vHi] := vT;
Inc(vLo);
Dec(vHi);
end;
until vLo > vHi;
if vHi > iLo then
IntArrayQuickSort(vIntArray, iLo, vHi);
if vLo < iHi then
IntArrayQuickSort(vIntArray, vLo, iHi);
end;
var
IntArray : TIntArray;
IntArrayLength : integer;
SortTime : LongWord;
begin
Randomize;
IntArrayAdjustLength(IntArrayLength, IntArray);
writeLn('les éléments avant TRI : ');
IntArrayWrite(IntArray);
SortTime := GetTickCount;
IntArrayQuickSort(IntArray, 0, IntArrayLength-1);
SortTime := GetTickCount-SortTime;
writeLn('durée du tri : ', SortTime,' millisecondes');
writeLn('les éléments après TRI : ');
IntArrayWrite(IntArray);
ReadLn;
end. |
Partager