Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- {$I SRL/osr.simba}
- procedure SelectionSort1(arr: TIntegerArray);
- var
- i, j, mi, hi: Int32;
- tmp: Int32;
- begin
- hi := High(arr)
- for i:=0 to hi-1 do
- begin
- mi := i;
- for j:=i+1 to hi do
- if arr[j] < arr[mi] then
- mi := j;
- if mi <> i then
- begin
- tmp:=arr[i];
- arr[i]:=arr[mi];
- arr[mi]:=tmp;
- end;
- end;
- end;
- procedure SelectionSort2(arr: TIntegerArray);
- var
- i, j, mi, hi: Int32;
- tmp: Int32;
- begin
- hi := High(arr);
- for i:=0 to hi-1 do
- begin
- mi := i;
- j := i+1;
- while j <= hi do
- begin
- if arr[j] < arr[mi] then
- mi := j;
- j := j + 1;
- end;
- if mi <> i then
- begin
- tmp:=arr[i];
- arr[i]:=arr[mi];
- arr[mi]:=tmp;
- end;
- end;
- end;
- procedure DoTest(n: Int32);
- var
- arr, orig: TIntegerArray;
- i: Int32;
- t:Double;
- begin
- for i:=0 to n do
- Insert(Int32(Random(0,$FFFFFF)), orig, 0);
- arr := Copy(orig);
- t := PerformanceTimer();
- SelectionSort1(arr);
- WriteLn('SelectionSort1: ', PerformanceTimer() - t,'ms');
- arr := Copy(orig);
- t := PerformanceTimer();
- SelectionSort2(arr);
- WriteLn('SelectionSort2: ', PerformanceTimer() - t,'ms');
- end;
- begin
- DoTest(5000);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement