Здраствуйте, столкнулся с такой проблемой: пишу сортировку на pascal (quicksort) и вроде бы все правильно (на мой взгляд), да вот только идет постоянно зацикливание (скорее всего проблема в самой рекурсии, т.к. без нее все работает без зацикливания). Вот код(с доп.выводом моих индексов i и j):
program quickSort;
{const blcok}
const
n = 7;
{type block}
type
massive = array[1..n] of integer;
{procedure block}
procedure input_arr(var arr:massive);
var
i: integer;
begin
writeln('vvedite elementi massiva: ');
for i := 1 to n do
begin
read(arr[i]);
end;
end;
procedure quicksort(var arr : massive);
var
lol1, lol2 : integer;
procedure sort(var l,r: integer); {body of my sort, include quicksort, but not declare it}
var
i,j,x,ch,g: integer;
begin
i := l;
j := r;
ch := (l+r) div 2;
x := arr[ch];
repeat
while arr[i] < x do i := i + 1;
while arr[j] > x do j := j - 1;
if i <= j then {if i <= j then}
begin
if arr[i] > arr[j] then
begin
arr[i] := arr[i] xor arr[j];
arr[j] := arr[i] xor arr[j];
arr[i] := arr[i] xor arr[j];
end;
end;
i := 1 + 1; j := j - 1;
for g := 1 to n do
begin
write(arr[g], ' ');
end;
write('nashi indexi: ');
write('eto nashe i: ', i, ' ');
write('eto nashe j: ', j);
writeln;
until i >= j;
if l < j then sort(l,j);
if i < r then sort(i,r);
end;
begin
lol1 := 1;
lol2 := 7;
sort(lol1,lol2);
end;
{var block}
var
i,j,mstart,mend: integer;
arr1 : massive;
BEGIN
input_arr(arr1);
write('eto nash massiv isnachalno: ');
for i:= 1 to n do
write(arr1[i], ' ');
writeln;
mstart := 1;
mend := 7;
quicksort(arr1);
write('eto nash OTSORTIROVANIY massiv: ');
for i:= 1 to n do
write(arr1[i], ' ');
END.
Допустим, мы ввели такие входные данные :
7 6 5 4 3 2 1
Тогда на выходе мы получаем вот что:
vvedite elementi massiva:
eto nash massiv isnachalno: 7 6 5 4 3 2 1
1 6 5 4 3 2 7 nashi indexi: eto nashe i: 2 eto nashe j: 6
1 2 5 4 3 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 5
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 4
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 3
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 2
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 0
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 3
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 2
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 3
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 2
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 3
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 2
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 3
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 2
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 3
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 2
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 3
1 2 3 4 5 6 7 nashi indexi: eto nashe i: 2 eto nashe j: 2
И так до бесконечности... Прошу помочь, т.к. скоро здавать а у меня совсем нет идей, где ошибка :((