Hi... I was wondering if I could get help with my Pascal program that is designed to quicksort an array.
I'm having difficulty compiling a Pascal program that performs a quicksort on an array. I keep getting the compiler error (using the Free Pascal compiler) that is telling me that the last end should be end; instead of end. I know this can't be true, because the ending of all Pascal programs must end with end.
Does anyone have any ideas on what the problem could be?
Thanks
I'm having difficulty compiling a Pascal program that performs a quicksort on an array. I keep getting the compiler error (using the Free Pascal compiler) that is telling me that the last end should be end; instead of end. I know this can't be true, because the ending of all Pascal programs must end with end.
Does anyone have any ideas on what the problem could be?
Thanks
Code:
program QS;
{ program's variables are declared }
var
i, done, length : Integer;
a: array[0..10] of Integer;
{ the quicksort function than will sort the array }
function Quicksort( left, right : Integer ) : Integer;
{ quicksort's local variables are declared }
var marker : Integer;
x : Integer;
y : Integer;
temp : Integer;
begin
while ( right - left ) > 0 do
begin
{ set the pivot to be the left element and placeholds x and y to be }
{ one less than and one greater than left and right, respectively }
marker := a[left];
x := left - 1;
y := right + 1;
{ infinite loop that will swap out two elements upon successful completion of an iteration }
while 1 = 1 do
begin
x := x + 1;
y := y - 1;
{ increment x while the element in a[x] is smaller than the marker }
while a[x] < marker do
begin
x := x + 1;
end;
{ decrement y while the element in a[y] is larger than the marker }
while a[y] > marker do
begin
x := x + 1;
end;
{ break out of the while loop of x and y overlap }
if x >= y then
break;
{ swap the data in a[x] and a[y] }
temp := a[x];
a[x] := a[y];
a[y] := temp;
end;
if y - left + 1 < right - y then
begin
{ recursive call to quicksort the subarray spanning from a[left] through a[y] }
done := Quicksort( left, y );
left := y + 1;
end
else
begin
{ recursive call to quicksort the subarray spanning from a[y+1] through a[right] }
done := Quicksort( y + 1, right );
right := y;
end;
{ output the array }
writeln( ' Implementing quicksort... ' );
while i <= length do
begin
writeln( a[i] );
i := i + 1;
end;
end;
{ begin the program here }
begin
i := 0;
length := 10; { the length is set to be 10 }
writeln( ' The array length is 10. Please enter the elements of this array: ' );
{ take in user input for each elememt of the array }
while i <= length do
begin
readln( a[i] );
i := i + 1;
end;
{ output the array }
writeln( ' The unsorted array: ' );
while i <= length do
begin
writeln( a[i] );
i := i + 1;
end;
done := Quicksort( 0, 10 );
i := 0;
{ output the array }
writeln( ' The sorted array: ' );
while i <= length do
begin
writeln( a[i] );
i := i + 1;
end;
end.