Delphi:排序列表 [英] Delphi : Sorted List

查看:81
本文介绍了Delphi:排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Delphi中对接近1,00,000个浮点条目进行排序.我是Delphi的新手,想知道是否有任何现成的解决方案可用.我尝试了几种语言提供的构造,它们花了非常长的时间才能完成.(5-10秒的执行时间对于应用程序来说很好)

I need to sort close to a 1,00,000 floating point entries in Delphi. I am new to Delphi and would like to know if there are any ready made solutions available. I tried a few language provided constructs and they take an inordinate amount of time to run to completion.(a 5-10 sec execution time is fine for the application)

推荐答案

为什么不仅仅实现快速排序算法?

why not just implement a quick Sort algorithm?

查看此简单代码

program ProjectSortFoat;

{$APPTYPE CONSOLE}

uses
  SysUtils;

procedure QuickSort(var List: array of Double; iLo, iHi: Integer) ;
var
  Lo       : integer;
  Hi       : integer;
  T        : Double;
  Mid      : Double;
begin
  Lo := iLo;
  Hi := iHi;
  Mid:= List[(Lo + Hi) div 2];
  repeat

    while List[Lo] < Mid do Inc(Lo) ;
    while List[Hi] > Mid do Dec(Hi) ;

    if Lo <= Hi then
    begin
      T := List[Lo];
      List[Lo] := List[Hi];
      List[Hi] := T;
      Inc(Lo);
      Dec(Hi);
    end;

  until Lo > Hi;

  if Hi > iLo then QuickSort(List, iLo, Hi);
  if Lo < iHi then QuickSort(List, Lo, iHi);

end;



const
Elements = 1000000;
var
  doubleArray : array of Double;
  i           : integer;
  t           : TDateTime;
begin
  SetLength(doubleArray,Elements);
  try
    t:=Now;
    Writeln('Init Generating '+FormatFloat('#,',Elements)+' random numbers ');
    for i:=low(doubleArray) to high(doubleArray) do
    doubleArray[i]:=Random(10000000)+Random; //can be improved
    Writeln('Elapsed '+FormatDateTime('HH:NN:SS.ZZZ',Now-t));

    t:=Now;
    Writeln('Sorting '+FormatFloat('#,',Elements)+' random numbers ');
    QuickSort(doubleArray, Low(doubleArray), High(doubleArray)) ;
    Writeln('Elapsed '+FormatDateTime('HH:NN:SS.ZZZ',Now-t));

  finally
  Finalize(doubleArray);
  end;
  Readln;
end.

在我的机器上,对1.000.000个浮点数进行排序的执行时间为 0.167秒.

in my machine, the execution time for sorting 1.000.000 float numbers is 0.167 seconds.

如果您有Delphi 7或其他旧版本(我不知道新版本中是否存在),则可以检查

if you have delphi 7 or another older version (i don't know if exist in the new versions) you can check the

C:\ Program文件\ Borland \ Delphi7 \ Demos \ Threads

C:\Program Files\Borland\Delphi7\Demos\Threads

路径,对于使用差值排序算法的酷演示应用程序来说,是一个线程.

path, for a cool demo app using differents sorting algorithms an threads.

这篇关于Delphi:排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆