Pascal Qsort

来源:百度知道 编辑:UC知道 时间:2024/05/28 02:32:36
这个Qsort是按照[算法导论]上的伪码写的,可是运行时是202错误[栈溢出]
我不知道怎么回事 同学和我一起的用Cpp写的过了 大家开一下帮帮忙 谢哦~
//-----------------------------------------
program qsort;
const
maxn=100000;
type
arr:array [1..maxn] of longint;
var
ae:arr;
n,i:longint;

function devide (var a:arr;p,r:longint):longint;
var
i,j,x,tmp:longint;
begin
x:=a[r];
i:=p-1;
for j:=p to r-1 do if a[j]<=x then begin
inc (i);
tmp:=a[i];a[i]:=a[j];a[j]:=tmp;
end;
tmp:=a[i+1];a[i+1]:=a[r];a[r]:=a[i+1];
devide:=i+1;
end;

procedure quick_sort(var s:arr;l,r:longint);
var
t:longint;
begin
t:=devide (s,l,r);
quick_sort (s,l,t-1);
quick_sort (s,t+1,r);
end;

begin
readln (n);
for i:=1 to n do read (ae[i]);
quick_sort (ae,1,n

在你的FP 的文件夹里找到\demo\text
里面有一个qsort.pp
这可是原带的,只不过数据是随机输入的,你自己在改一改好了,如果你懒得找这个文件我给你好了:(最好自己找一找,那个文件夹里还有其他好东西)
{
This file is part of the Free Pascal run time library.
Copyright (c) 1993-2005 by the Free Pascal Development Team

QuickSort Example

See the file COPYING.FPC, included in this distribution,
for details about the copyright.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

**********************************************************************}
program quicksort;

const
{$ifndef MACOS}
max = 100000;
{$else}
max = 1000; {Actually it works with 100000 also, but that might }
{lead problems occacionally.}
{$endif}

type
tlist = array[1..