我的快速程序似乎没有做任何事情

时间:2014-12-06 13:54:55

标签: pascal quicksort procedure

我尝试将我的c ++代码转换为Pascal,并且我确定我的代码正在运行。转换后,一切顺利,除了结果与我的c ++代码不一样直接 - 它不会对c ++中的任何东西进行排序。 这是我的Pascal:

program try_quicksorting;

uses crt;

  var
num: array[1..20] of integer;
x, y, a: integer;
again: char;


procedure quickSort(arr: array of integer; left, right:integer);

var
    i:integer; {pointer for my left side}
    j:integer; {pointer for my right side}
    tmp:integer;
    pivot:integer;
begin

i:=left;
j:=right;

  pivot := arr[(left + right) div 2];

      while (i <= j) do
        begin
        while (arr[i] < pivot) and (arr[j] < pivot) do
        i:=i+1; 
    while (arr[j] > pivot) and (arr[i] > pivot) do
        j:=j-1;

     if(i <= j) then
    begin

            tmp:=arr[i];
                    arr[i]:=arr[j];
                    arr[j]:=tmp;
                    i:=i+1;
                    j:=j-1;

    end;

        end;

 if j > left then
   quickSort(arr, left, j);
if i < right then
   quickSort(arr, i, right);


 end;
 begin

    clrscr;
x:=1;
repeat
    clrscr;


    writeln('******************************************************');
    write('Records: ');

    a:=1;
    while(a<x) do
            begin
     write( num[a], ' ');
    a:=a+1;
    end;

    writeln('');
    writeln('******************************************************');
    write('Enter a number: ');
        readln(num[x]);
    writeln('------------------------------------------------------');


repeat
    write('Again[Y/N]: ');
                read(again);

           if(again = 'y') or (again = 'Y') or (again = 'n') or (again = 'N')then
        x:=x+1
           else
               writeln('Invalid input! Please press Y for Yes and N for No.');

 until (again = 'y') or (again = 'Y') or (again = 'n') or (again = 'N');

 until (again = 'n') or (again = 'N');
 readln;


quickSort(num,1,x);

write('Sorted Form: ');
y:=1;
while (y<x) do
    begin
        write(num[y], ' ');
        y:=y+1;
    end;
    readln;
end.

那就是它。我希望你的回应立即和善意:)

1 个答案:

答案 0 :(得分:0)

在普通的Pascal中,数组是值类型,所以在这

procedure quickSort(arr: array of integer; left, right:integer);

您正在传递数组的副本。

更改为

procedure quickSort(var arr: array of integer; left, right:integer);

它应该会更好。