电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

极快的正整数排序函数


发布日期:2021/7/29
 

实现原理 对比二进制位

unitUnit;

interface

uses

WindowsMessagesSysUtilsVariantsClassesGraphicsControlsForms

DialogsStdCtrls;

type

TForm=class(TForm)

Button:TButton;

Memo:TMemo;

procedureButtonClick(Sender:TObject);

procedureFormCreate(Sender:TObject);

end;

var

Form:TForm;

implementation

{$R*dfm}

type

TIntArr=arrayofInteger;

{极快的正整数排序函数}

procedureIntSort(arr:TIntArr;low:Integer=;high:Integer=;k:Cardinal=$;c:Cardinal=);

var

ijx:Integer;

begin

ifhigh=thenhigh:=Length(arr);

i:=low;

j:=high;

while(i<j)do

begin

while(arr[j]andk<>)and(i<j)doDec(j);

while(arr[i]andk=)and(i<j)doInc(i);

ifi<jthen

begin

x:=arr[j];

arr[j]:=arr[i];

arr[i]:=x;

endelsebegin

ifarr[j]andk<>thenDec(i)elseInc(j);

Break;

end;

end;

ifk>cthen

begin

iflow<ithenIntSort(arrlowikdiv);

ifj<highthenIntSort(arrjhighkdiv);

end;

end;

{测试}

procedureTFormButtonClick(Sender:TObject);

var

MyArr:TIntArr;

i:Integer;

t:Int;

begin

SetLength(MyArrMAXWORD);

fori:=Low(MyArr)toHigh(MyArr)doMyArr[i]:=Random(MaxInt);

t:=GetTickCount;

IntSort(MyArr);//调用排序函数

Text:=IntToStr(GetTickCountt);

MemoClear;

fori:=toLength(MyArr)do

begin

ifimod=then

MemoLinesAdd(IntToStr(MyArr[i]));

end;

end;

procedureTFormFormCreate(Sender:TObject);

begin

MemoClear;

MemoAlign:=alLeft;

MemoScrollBars:=ssVertical;

end;

上一篇:为何选择Indy?

下一篇:字段对象的概念和字段对象的建立