Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.93 KB | None | 0 0
  1. program QuickSORT;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8.   System.SysUtils;
  9.  
  10. type
  11.    TArray = array of Integer;
  12.  
  13. function QuickSort_(UserArr: TArray): TArray;
  14. var
  15.    LeftPart, RigthPart, MidPart: TArray;
  16.    Iteration, i, LeftLen, RigthLen, MidElement, MidLen: Integer;
  17.    LeftIndex, RigthIndex, MidIndex: Integer;
  18. begin
  19.    if length(UserArr) < 2 then
  20.       QuickSort_ := UserArr
  21.    else
  22.    begin
  23.       MidElement := UserArr[(Length(UserArr) - 1) div 2];
  24.       LeftLen := 0;
  25.       RigthLen := 0;
  26.       MidLen := 0;
  27.       Iteration := length(UserArr) - 1;
  28.       for i := 0 to Iteration do
  29.       begin
  30.          if UserArr[i] > MidElement then
  31.             Inc(RigthLen)
  32.          else
  33.             if UserArr[i] < MidElement then
  34.                Inc(LeftLen)
  35.             else
  36.                Inc(MidLen)
  37.       end;
  38.       SetLength(LeftPart, LeftLen);
  39.       SetLength(RigthPart, RigthLen);
  40.       SetLength(MidPart, MidLen);
  41.       LeftIndex := 0;
  42.       RigthIndex := 0;
  43.       MidIndex := 0;
  44.       for i := 0 to Iteration do
  45.       begin
  46.          if UserArr[i] > MidElement then
  47.          begin
  48.             RigthPart[RigthIndex] := UserArr[i];
  49.             Inc(RigthIndex)
  50.          end
  51.          else
  52.             if UserArr[i] < MidElement then
  53.             begin
  54.                LeftPart[LeftIndex] := UserArr[i];
  55.                Inc(LeftIndex)
  56.             end
  57.             else
  58.             begin
  59.                MidPart[MidIndex] := UserArr[i];
  60.                Inc(MidIndex)
  61.             end;
  62.       end;
  63.       QuickSort_ := QuickSort_(LeftPart) + MidPart  + QuickSort_(RigthPart);
  64.    end;
  65. end;
  66.  
  67.  
  68.  
  69. var
  70.    UserArr: TArray;
  71.    N, i, Iteration: Integer;
  72. begin
  73.    Writeln('Enter N');
  74.    Readln(N);
  75.    SetLength(UserArr, N);
  76.    Iteration := N - 1;
  77.    for i := 0 to Iteration do
  78.       Read(UserArr[i]);
  79.    UserArr := QuickSort_(UserArr);
  80.    for i := 0 to Iteration do
  81.       Write(UserArr[i], ' ');
  82.    Readln;
  83.    Readln;
  84. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement