大榕树——让我们共成长!
大榕树 myDrs.org
您的位置:大榕树 \ 编程       |  Logo语言   |  Pascal语言   |  信息学奥赛   |  高考保送    |  HTML版本
|  Pascal语言>>Pascal练习>>快速排序         本站全文搜索: 友情提示:

快速排序
http://www.mydrs.org  6/21/2001  大榕树


快速排序

[问题来源] Borland Pascal Examples
[问题描述] 快速排序是至今为止人们发现的最快的排序方法,它采用了类似折半查找的方法:把一组数据分为两组,其中一组比某一数据大,另一组比该数据小.然后分别在进行该步骤,直到每组都剩下一个数为止.
输入:[NONE] 输出:[SCREEN]

{************************************************}
{ }
{ QuickSort Demo }
{ Copyright (c) 1985,90 by Borland International }
{ }
{************************************************}

program QSort;
{$R-,S-}
uses Crt;

{ This program demonstrates the quicksort algorithm, which }
{ provides an extremely efficient method of sorting arrays in }
{ memory. The program generates a list of 1000 random numbers }
{ between 0 and 29999, and then sorts them using the QUICKSORT }
{ procedure. Finally, the sorted list is output on the screen. }
{ Note that stack and range checks are turned off (through the }
{ compiler directive above) to optimize execution speed. }

const
Max = 1000;

type
List = array[1..Max] of Integer;

var
Data: List;
I: Integer;

{ QUICKSORT sorts elements in the array A with indices between }
{ LO and HI (both inclusive). Note that the QUICKSORT proce- }
{ dure provides only an "interface" to the program. The actual }
{ processing takes place in the SORT procedure, which executes }
{ itself recursively. }

procedure QuickSort(var A: List; Lo, Hi: Integer);

procedure Sort(l, r: Integer);
var
i, j, x, y: integer;
begin
i := l; j := r; x := a[(l+r) DIV 2];
repeat
while a[i] < x do i := i + 1;
while x < a[j] do j := j - 1;
if i <= j then
begin
y := a[i]; a[i] := a[j]; a[j] := y;
i := i + 1; j := j - 1;
end;
until i > j;
if l < j then Sort(l, j);
if i < r then Sort(i, r);
end;

begin {QuickSort};
Sort(Lo,Hi);
end;

begin {QSort}
Write('Now generating 1000 random numbers...');
Randomize;
for i := 1 to Max do Data[i] := Random(30000);
Writeln;
Write('Now sorting random numbers...');
QuickSort(Data, 1, Max);
Writeln;
for i := 1 to 1000 do Write(Data[i]:8);
end.

作 者:MQL
来 源:Pascal Zone
共有5270位读者阅读过此文

  • 上篇文章折半查找
  • 下篇文章约瑟夫环

  • 发送邮件
    保存页面 打印文章 HTML版本 发表评论

    □- 近期热门文章 □- 相关文章
    1. NOIP2006竞赛大纲 [8306]
    2. 七类高中生具有保送资格 [5910]
    3. NOI2006获奖选手名单 [4955]
    4. 关于举办NOIP2006模拟赛的通告 [4106]
    5. Turbo Pascal各语句运行速... [3594]
    6. Turbo王者归来新Delphi免费... [3181]
    7. IOI2006我国4名选手全部获得金... [2945]
    8. 关于APIO2007与IOI2007... [2763]
    9. noip倒计时 by 枯叶蝴蝶 [2683]
    10. 朱泽园:思想上的金牌更重要 [2168]
    快速排序
    折半查找
     

    关于本站 | 合作伙伴 | 联系方式
    大榕树 版权所有 ©1999-2006 www.myDrs.org 闽ICP备05000721号