0% found this document useful (0 votes)
18 views

Quicksortarray: Procedure Var Var

This document describes a recursive quicksort algorithm that sorts an array. It uses three pointers - movable, immovable, and temporary - to iterate through the array and swap elements that are out of order until the array is fully sorted. The algorithm recursively calls itself to continue sorting smaller subsections of the array until the entire array is in sorted order.

Uploaded by

raperd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Quicksortarray: Procedure Var Var

This document describes a recursive quicksort algorithm that sorts an array. It uses three pointers - movable, immovable, and temporary - to iterate through the array and swap elements that are out of order until the array is fully sorted. The algorithm recursively calls itself to continue sorting smaller subsections of the array until the entire array is in sorted order.

Uploaded by

raperd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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;

You might also like