This is a poor implementation of quicksort in pascal.
I will use it in the classroom for a demonstration in
sorting algorithms (divide and conquer).
I would appreciate if someone could help me a little with some improvements.

``````program quicksort;
uses wincrt;
const max = 10;
type table = array [1..max] of Integer;
var x , i, downlimit , uplimit:integer;
matrix:table;
flag:boolean;
begin
for i := 1 to max do
begin
writeln('GIVE MATRIX[' , i , ']');
end
end;

procedure writeArr( VAR matrix: table;downlimit , uplimit:integer);
var i : integer;
begin
for i := downlimit to uplimit do
begin
writeln(' matrix[' , i , '] = ' , matrix[i]);
end
end;

procedure swap(var a,b : integer);
var c:integer;
begin
c:=a;
a:=b;
b:=c;
end;

procedure qsort(var matrix : table ; downlimit , uplimit: integer);
var middle , i , j : integer;
begin
flag := false;
i:= downlimit ;
j:= uplimit;
middle:= (downlimit + uplimit) div 2 ;
x:= matrix[middle];
writeln;
writeln('i , j are : ' , i, '  '  , j );
writeln('x is :' , x);
writeln;
while(i<j)  do
begin

while matrix[i] < x do  begin  writeln('matrix[' , i ,'] = ' , matrix[i] , ' < ' , x ); i:=i+1;  end;

while matrix[j] > x do  begin  writeln('matrix[' , j , '] :' , matrix[j], ' > ' , x ); j:=j-1;  end;

if( i< j) then
begin
swap(matrix[i] , matrix[j]);
i:= i+1; j:= j-1;
flag := true;
end;
writeArr(matrix ,downlimit , uplimit );
end;
if (uplimit > i)and (downlimit < j) and (flag = true) then
begin
qsort(matrix , i , uplimit);
qsort(matrix , downlimit , j);
end;

end;  //  PROCEDURE

begin //MAIN PROGRAM
downlimit :=1;
uplimit:=max;
flag:= false;