Advertisement
WarPie90

Untitled

Apr 17th, 2022
1,670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.26 KB | None | 0 0
  1. program new;
  2. {$I SRL/osr.simba}
  3.  
  4. procedure SelectionSort1(arr: TIntegerArray);
  5. var
  6.   i, j, mi, hi: Int32;
  7.   tmp: Int32;
  8. begin
  9.   hi := High(arr)
  10.   for i:=0 to hi-1 do
  11.   begin
  12.     mi := i;
  13.     for j:=i+1 to hi do
  14.       if arr[j] < arr[mi] then
  15.         mi := j;
  16.  
  17.     if mi <> i then
  18.     begin
  19.       tmp:=arr[i];
  20.       arr[i]:=arr[mi];
  21.       arr[mi]:=tmp;
  22.     end;
  23.   end;
  24. end;
  25.  
  26.  
  27. procedure SelectionSort2(arr: TIntegerArray);
  28. var
  29.   i, j, mi, hi: Int32;
  30.   tmp: Int32;
  31. begin
  32.   hi := High(arr);
  33.   for i:=0 to hi-1 do
  34.   begin
  35.     mi := i;
  36.     j := i+1;
  37.     while j <= hi do
  38.     begin
  39.       if arr[j] < arr[mi] then
  40.         mi := j;
  41.       j := j + 1;
  42.     end;
  43.  
  44.     if mi <> i then
  45.     begin
  46.       tmp:=arr[i];
  47.       arr[i]:=arr[mi];
  48.       arr[mi]:=tmp;
  49.     end;
  50.   end;
  51. end;
  52.  
  53. procedure DoTest(n: Int32);
  54. var
  55.   arr, orig: TIntegerArray;
  56.   i: Int32;
  57.   t:Double;
  58. begin
  59.   for i:=0 to n do
  60.     Insert(Int32(Random(0,$FFFFFF)), orig, 0);
  61.  
  62.   arr := Copy(orig);
  63.   t := PerformanceTimer();
  64.   SelectionSort1(arr);
  65.   WriteLn('SelectionSort1: ', PerformanceTimer() - t,'ms');
  66.  
  67.   arr := Copy(orig);
  68.   t := PerformanceTimer();
  69.   SelectionSort2(arr);
  70.   WriteLn('SelectionSort2: ', PerformanceTimer() - t,'ms');
  71. end;
  72.  
  73. begin
  74.   DoTest(5000);
  75. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement