Skip to content

Instantly share code, notes, and snippets.

@Winwardo
Created October 5, 2011 14:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Winwardo/1264571 to your computer and use it in GitHub Desktop.
Save Winwardo/1264571 to your computer and use it in GitHub Desktop.
Quicksort algorithm in Pascal
program quicksort;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, windows;
{$R *.res}
type tNameArray = array of string;
procedure quickSortArray(var inputArray : tNameArray; lowPos, highPos : integer);
//This is a recursive quicksort that cuts the array down into smaller parts to be sorted each pass
var
movablePointer, immovablePointer, temporaryPointer : integer;
temporaryItem : string;
begin
immovablePointer := lowPos; //Set the immovable pointer to the lowest input part of the array
movablePointer := highPos; //Set the movable pointer to the highest input part of the array
while (movablePointer <> immovablePointer) do
begin
if(movablePointer > immovablePointer) then
//While the movable pointer is larger than the immovable, slowly move it towards the immovable, checking each step if
//the value at that position is larger than the immovable's position.
begin
if(inputArray[movablePointer] < inputArray[immovablePointer]) then //If they are in the wrong order, swap them!
begin
temporaryItem := inputArray[movablePointer]; //Exchange the values in the array with a temporary value
inputArray[movablePointer] := inputArray[immovablePointer];
inputArray[immovablePointer] := temporaryItem;
temporaryPointer := movablePointer; //Exchange the positions to check
movablePointer := immovablePointer;
immovablePointer := temporaryPointer;
end
else
begin
dec(movablePointer); //Otherwise move the movable clsoer to the immovable
end;
end
else
begin
//Similar as above, but in the other direction
if(inputArray[movablePointer] > inputArray[immovablePointer]) then
begin
temporaryItem := inputArray[movablePointer];
inputArray[movablePointer] := inputArray[immovablePointer];
inputArray[immovablePointer] := temporaryItem;
temporaryPointer := movablePointer;
movablePointer := immovablePointer;
immovablePointer := temporaryPointer;
end
else
begin
inc(movablePointer);
end;
end;
//windows.beep(movablePointer * 40,20);
end;
//Now recursively check the lower and higher up parts of the array to sort
if(movablePointer > lowPos) then
quickSortArray(inputArray,lowPos,movablePointer-1);
if(movablePointer < highPos) then
quickSortArray(inputArray,movablePointer+1,highPos);
end;
var
nameArray : tNameArray;
count : integer;
begin
setLength(nameArray,100);
nameArray[0] := 'chris';
nameArray[1] := 'bob';
nameArray[2] := 'anna';
nameArray[3] := 'max';
nameArray[4] := 'tom';
nameArray[5] := 'fran';
nameArray[6] := 'michael';
nameArray[7] := 'john';
nameArray[8] := 'alex';
nameArray[9] := 'kirsty';
for count := 0 to high(nameArray) do
begin
nameArray[count] := char(round(random*20));
end;
quickSortArray(nameArray,low(nameArray),high(nameArray));
for count := 0 to high(nameArray) do
begin
writeln(nameArray[count]);
end;
readln;
end.
@QuantumQuacken
Copy link

The best recursive quicksort made in pascal that i have ever found. Good job and thank you!

@timoransky
Copy link

:P very nice such quick much sort so wow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment