各类排序总结(DELPHI版)
备忘一下
排序
TSortType = array of integer;
procedure Swap(var sorttemp : TSortType;left,right:integer);
var temp :integer;
begin
temp := sorttemp[left];
sorttemp[left]:= sorttemp[right];
sorttemp[right] := temp;
end;
function PartitionIt(var sorttemp : TSortType;left,right,pivot:integer):integer;
var leftptr,rightptr :integer;
begin
leftptr := left -1;
rightptr := right ;
while true do begin
while leftptr < length(sorttemp) do begin
leftptr := leftptr+1;
if sorttemp[leftptr] >= pivot then break;
end;
while true do begin
rightptr:= rightptr-1;
if rightptr < 0 then break;
if sorttemp[rightptr] <= pivot then break;
end;
if (leftptr >= rightptr) then break;
swap(sorttemp,leftptr,rightptr);
end;
swap(sorttemp,leftptr,right);
result:= leftptr;
end;
procedure QuickSort(var sorttemp : TSortType;left,right:integer); //快速排序
var pivot,partition:integer;
begin
if (left-right) > 0 then exit;
pivot := sorttemp[right];
partition := partitionit(sorttemp,left,right,pivot);
quicksort(sorttemp,left,partition-1);
quicksort(sorttemp,partition+1,right);
end;
function CopyArray(SortO : TSortType):TSortType;
var i,len:integer;
Sorttemp : TSortType;
begin
len := length(SortO);
SetLength(Sorttemp,len);
for i:=0 to len-1 do begin
Sorttemp[i] := SortO[i];
end;
result := Sorttemp;
end;
procedure InsertionSort(var sorttemp :Tsorttype); //插入排序
var i,outer,inner,temp,len:integer;
begin
len := length(sorttemp);
for outer:=0 to len-1 do begin
inner := outer;
temp:=sorttemp[outer];
while (inner>0) and (sorttemp[inner-1]>=temp ) do begin
sorttemp[inner]:=sorttemp[inner-1];
inner := inner-1;
end;
sorttemp[inner] := temp;
end;
end;
procedure ShellSort(var sorttemp :Tsorttype); //希尔排序
var i,h,inner,outer,temp,len:integer;
begin
h:=1;
len := length(sorttemp);
while(h<=len/3)do begin
h := h*3+1;
end;
while(h>0)do begin
for outer:=h to len-1 do begin
inner := outer;
temp := sorttemp[outer];
while((inner>h-1)and (sorttemp[inner-h]>=temp))do begin
sorttemp[inner]:=sorttemp[inner-h];
inner := inner-h;
end;
sorttemp[inner]:=temp;
end;
h:=(h-1) div 3;
end;
end;
procedure BubbleSort(var Sorttemp: TSortType); //冒泡排序
var temp,len,outer,inner:integer;
begin
temp := 0;
len := length(Sorttemp);
for outer:=len-1 downto 1 do begin
for inner := 0 to outer-1 do begin
if sorttemp[inner] > sorttemp[inner+1] then begin
temp := sorttemp[inner];
sorttemp[inner] := sorttemp[inner+1];
sorttemp[inner+1] := temp;
end;
end;
end;
end;
procedure AdvanceBubbleSort(var Sorttemp: TSortType); //改进冒泡排序
var temp,len,outer,inner:integer;
flag :boolean;
begin
temp := 0;
len := length(Sorttemp);
for outer:=len-1 downto 1 do begin
flag := false;
for inner := 0 to outer-1 do begin
if sorttemp[inner] > sorttemp[inner+1] then begin
temp := sorttemp[inner];
sorttemp[inner] := sorttemp[inner+1];
sorttemp[inner+1] := temp;
flag :=true;
end;
end;
if not flag then break;
end;
end;
TSortType = array of integer;
procedure Swap(var sorttemp : TSortType;left,right:integer);
var temp :integer;
begin
temp := sorttemp[left];
sorttemp[left]:= sorttemp[right];
sorttemp[right] := temp;
end;
function PartitionIt(var sorttemp : TSortType;left,right,pivot:integer):integer;
var leftptr,rightptr :integer;
begin
leftptr := left -1;
rightptr := right ;
while true do begin
while leftptr < length(sorttemp) do begin
leftptr := leftptr+1;
if sorttemp[leftptr] >= pivot then break;
end;
while true do begin
rightptr:= rightptr-1;
if rightptr < 0 then break;
if sorttemp[rightptr] <= pivot then break;
end;
if (leftptr >= rightptr) then break;
swap(sorttemp,leftptr,rightptr);
end;
swap(sorttemp,leftptr,right);
result:= leftptr;
end;
procedure QuickSort(var sorttemp : TSortType;left,right:integer); //快速排序
var pivot,partition:integer;
begin
if (left-right) > 0 then exit;
pivot := sorttemp[right];
partition := partitionit(sorttemp,left,right,pivot);
quicksort(sorttemp,left,partition-1);
quicksort(sorttemp,partition+1,right);
end;
function CopyArray(SortO : TSortType):TSortType;
var i,len:integer;
Sorttemp : TSortType;
begin
len := length(SortO);
SetLength(Sorttemp,len);
for i:=0 to len-1 do begin
Sorttemp[i] := SortO[i];
end;
result := Sorttemp;
end;
procedure InsertionSort(var sorttemp :Tsorttype); //插入排序
var i,outer,inner,temp,len:integer;
begin
len := length(sorttemp);
for outer:=0 to len-1 do begin
inner := outer;
temp:=sorttemp[outer];
while (inner>0) and (sorttemp[inner-1]>=temp ) do begin
sorttemp[inner]:=sorttemp[inner-1];
inner := inner-1;
end;
sorttemp[inner] := temp;
end;
end;
procedure ShellSort(var sorttemp :Tsorttype); //希尔排序
var i,h,inner,outer,temp,len:integer;
begin
h:=1;
len := length(sorttemp);
while(h<=len/3)do begin
h := h*3+1;
end;
while(h>0)do begin
for outer:=h to len-1 do begin
inner := outer;
temp := sorttemp[outer];
while((inner>h-1)and (sorttemp[inner-h]>=temp))do begin
sorttemp[inner]:=sorttemp[inner-h];
inner := inner-h;
end;
sorttemp[inner]:=temp;
end;
h:=(h-1) div 3;
end;
end;
procedure BubbleSort(var Sorttemp: TSortType); //冒泡排序
var temp,len,outer,inner:integer;
begin
temp := 0;
len := length(Sorttemp);
for outer:=len-1 downto 1 do begin
for inner := 0 to outer-1 do begin
if sorttemp[inner] > sorttemp[inner+1] then begin
temp := sorttemp[inner];
sorttemp[inner] := sorttemp[inner+1];
sorttemp[inner+1] := temp;
end;
end;
end;
end;
procedure AdvanceBubbleSort(var Sorttemp: TSortType); //改进冒泡排序
var temp,len,outer,inner:integer;
flag :boolean;
begin
temp := 0;
len := length(Sorttemp);
for outer:=len-1 downto 1 do begin
flag := false;
for inner := 0 to outer-1 do begin
if sorttemp[inner] > sorttemp[inner+1] then begin
temp := sorttemp[inner];
sorttemp[inner] := sorttemp[inner+1];
sorttemp[inner+1] := temp;
flag :=true;
end;
end;
if not flag then break;
end;
end;