冒泡排序和折半查找(100分)

  • 主题发起人 主题发起人 czly
  • 开始时间 开始时间
C

czly

Unregistered / Unconfirmed
GUEST, unregistred user!
谢谢写下代码
 
//冒泡排序
procedure fSort(var inX:array of double)

var temp:double

i,j,n:integer

flag:boolean

Begin
for i:=Low(inX) to High(inX) do
for j := Low(inX) to High(inX) - 1 do
if inX[j] > inX[j + 1] then
begin
temp := inX[j]

inX[j] := inX[j + 1]

inX[j + 1] := temp

end

end;

//折半查找:
function BinarySearch(A: Array of integer
low:integer
high:integer
k:integer ):integer
//假设A是升序排列的
var
iLow,iHigh,iMid: integer
// 折半的下界,上界,和折半的中间值
begin
iLow:= low

iHigh:= high

iMid:= (iLow + iHigh) div 2

while (iLow <> iHigh) do
begin
if A[iMid] > K then
iHigh:= iMid

if A[iMid] < K then
iLow:= iMid

if A[iMid] = K then
begin
Result:= iMid

Break

end

iMid:= (iLow + iHigh) div 2
// 保证循环用
end

if (iLow = iHigh) then
result:= -1
// 返回-1表明没有找到
end;
 
//冒泡排序
function bubblesort(a:tintarray):tintarray;
var
i ,j,temp:integer;
begin
for i:=low(a) to haigh(a)-1 do
for j:=low(a) to (high(a)-1)-i do
if a [j]<a[j+1] then
begin
temp:=a[j];
a[j]:=a[j+1];
a[j+1]:=temp;
end;
result:=a;
end;

//折半查找

function binsearch(a:tintarray;k:integer):integer;
var
l,h,mid:integer;
found:boolean;
begin
l:=low(a);
h:=high(a);
found:=false;
while (l<=h)and not found do
begin
mid:=round((l+h)/2);
if k<a[mid] then
l:=mid+1
else
if k=a[mid] then
found:=true
else
h:=mid-1;
end;
if found then
result:=mid
else
result:=-1;
end;
 
二楼第二个函数改了才对
 
后退
顶部