1. 程式人生 > >python 插入查找

python 插入查找

urn ret 位置 true erp pri h+ num NPU

def interpolation_search(data,val):
    low=0
    high=len(data)-1
    print(查找過程中......)
    while low<= high and val !=-1:
        mid=low+int((val-data[low])*(high-low)/(data[high]-data[low])) #插值查找法公式
        if val==data[mid]:
            return mid
        elif val < data[mid]:
            print(
%d 介於位置 %d[%3d] 和中間值 %d[%3d] 之間,找左半邊 %(val,low+1,data[low],mid+1,data[mid])) high=mid-1 elif val > data[mid]: print(%d 介於中間值位置 %d[%3d] 和 %d[%3d] 之間,找右半邊 %(val,mid+1,data[mid],high+1,data[high])) low=mid+1 return
-1 val=1 data=[3,4,5,6,7,8,9] while True: num=0 val=int(input(請輸入查找鍵值(1-150),輸入-1結束:)) if val==-1: break num=interpolation_search(data,val) if num==-1: print(##### 沒有找到[%3d] ##### %val) else: print(在第 %2d個位置找到 [%3d] %(num+1,data[num])) print(
數據內容為:) for j in data: print(%3d %(j),end=‘‘) print()

python 插入查找