順序查詢和二分查詢法複雜度的差異

2021-10-03 22:23:18 字數 585 閱讀 7553

1.問題

寫出兩種檢索演算法:在乙個排好序的陣列t[1…n]中查詢x,如果x在t中,輸出x在

t的下標j;如果x不在t中,輸出j=0。

2.分析

兩種檢索演算法,我們選擇1.順序查詢法。2.二分查詢法。

3.偽**

順序查詢法:for

(int i =

0; i < n; i++)}

二分查詢法:int top =

0, end = n -

1, mid;

int find =-1

;while

(top <= end)

else

if(t[mid]

> x)

else

end = top +1;

}

4.分析

順序查詢法

複雜度:o(n)

二分查詢法

複雜度:o(log n)

5.原始碼

時間複雜度 二分查詢

不常見的時間複雜度 看看就好 如何一眼判斷時間複雜度?def hanoi n,a,b,c if n 0 hanoi n 1,a,c,b print s s a,c hanoi n 1,b,a,c hanoi 5,a b c h n 2h n 1 1 h 1 1遞迴面試題 斐波那契 斐波那契 數列 f...

順序查詢法和二分查詢法

方法1 def shunxu f list,temp for index,i in enumerate f list if i temp return index return none方法2 def shunxu f flist,temp for i in range len f flist if...

01 複雜度3 二分查詢

position binarysearch list l,elementtype x 其中list結構定義如下 typedef int position typedef struct lnode list struct lnode l是使用者傳入的乙個線性表,其中elementtype元素可以通過 ...