Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program QuickSORT;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- TArray = array of Integer;
- function QuickSort_(UserArr: TArray): TArray;
- var
- LeftPart, RigthPart, MidPart: TArray;
- Iteration, i, LeftLen, RigthLen, MidElement, MidLen: Integer;
- LeftIndex, RigthIndex, MidIndex: Integer;
- begin
- if length(UserArr) < 2 then
- QuickSort_ := UserArr
- else
- begin
- MidElement := UserArr[(Length(UserArr) - 1) div 2];
- LeftLen := 0;
- RigthLen := 0;
- MidLen := 0;
- Iteration := length(UserArr) - 1;
- for i := 0 to Iteration do
- begin
- if UserArr[i] > MidElement then
- Inc(RigthLen)
- else
- if UserArr[i] < MidElement then
- Inc(LeftLen)
- else
- Inc(MidLen)
- end;
- SetLength(LeftPart, LeftLen);
- SetLength(RigthPart, RigthLen);
- SetLength(MidPart, MidLen);
- LeftIndex := 0;
- RigthIndex := 0;
- MidIndex := 0;
- for i := 0 to Iteration do
- begin
- if UserArr[i] > MidElement then
- begin
- RigthPart[RigthIndex] := UserArr[i];
- Inc(RigthIndex)
- end
- else
- if UserArr[i] < MidElement then
- begin
- LeftPart[LeftIndex] := UserArr[i];
- Inc(LeftIndex)
- end
- else
- begin
- MidPart[MidIndex] := UserArr[i];
- Inc(MidIndex)
- end;
- end;
- QuickSort_ := QuickSort_(LeftPart) + MidPart + QuickSort_(RigthPart);
- end;
- end;
- var
- UserArr: TArray;
- N, i, Iteration: Integer;
- begin
- Writeln('Enter N');
- Readln(N);
- SetLength(UserArr, N);
- Iteration := N - 1;
- for i := 0 to Iteration do
- Read(UserArr[i]);
- UserArr := QuickSort_(UserArr);
- for i := 0 to Iteration do
- Write(UserArr[i], ' ');
- Readln;
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement