mastaraymond
2007-12-17 14:30:40
Well, i made some functions related to Radix Sort.. Its quite effective way of sorting things..
Try to understand it ^^
Try to understand it ^^
program Hoi;
Const
Radix = 10; //I can't really explain.. Radix 10 is used the most.. It stands for 0,1,2,3,4,5,6,7,8,9.. And if your using negative numbers to, its -9..9
Procedure RadixSort(Integers : TIntegerArray; Digits : Integer; Negative : Boolean);
var
Dump : Array[-Radix + 1..Radix-1] of TIntegerArray; //We store them temporary here
Counts : Array[-Radix + 1..Radix-1] of Integer; //Counts, for the length of the Dump
Len,Divider,I,II,III,Temporary,Counter : Integer;
RadixMax,RadixMin : integer;
begin
Divider := 1; //We use this, to sort all the digits time by time ;).
Len := High(Integers);
if Negative then
RadixMin := -Radix + 1
else
RadixMin := 0;
RadixMax := Radix - 1;
For I := RadixMin to RadixMax do
SetLength(Dump[I],Len + 1);
For I := 1 to Digits do //Digits!
begin;
For II := RadixMin to RadixMax do
Counts[II] := -1; //Reset the counts
For II := 0 to Len do
begin;
Temporary := (Integers[II] div Divider) mod Radix // We get the single digit of the number..
Counts[Temporary] := Counts[Temporary] + 1; //Increase the Counts ;)
Dump[Temporary][Counts[Temporary]] := Integers[II]; //Store the whole number in in its own digit 'dump', 0 to Radix - 1.. E.G. : Dump[0][0] := Integers[II];
end;
Counter := -1;
For II := RadixMin to RadixMax do
For III := 0 to Counts[II] do
begin;
Counter := Counter + 1;
Integers[Counter] := Dump[II][III]; //Place back the orignal var, "Integers"
end;
Divider := Divider * Radix; //So that we go to the next Digit
end;
end;
Function GetDigits( Num : Integer):Integer;
begin;
if Num = 0 then Result := 1;
While Num <> 0 do
begin;
Result := Result + 1;
Num := Num div 10;
end;
end;
Function GetDigitsArray ( Ints : TIntegerArray) : Integer;
var
C,I : Integer;
Len : Integer;
begin;
Len := High(Ints);
if Len < 0 then Exit;
C := 1;
For I := 0 to Len do
if (Ints[I] >= C) or (Ints[I] <= -C) then
begin;
C := C * 10;
Dec(I);
end;
Result := GetDigits(C - 1);
end;
var
Ints : TIntegerArray;
I : integer;
Time : Integer;
begin
Writeln(inttostr(GetDigits(100)));
Writeln(inttostr(GetDigitsArray([15,0,10,-99])));
// exit;
SetLength(Ints,1000);
For I := 0 to 999 do
Ints[I] := Random(999) - Random(999);
Time := GetSystemTime;
RadixSort(Ints,GetDigitsArray(Ints),True);
Time := GetSystemTime - Time;
For I := 0 to high(ints) do
Writeln(inttostr(ints[I]));
Writeln(IntToStr(Time) + ' msec - RadixSort with Digit finding!');
end.