01 複雜度3 二分查詢

2021-09-27 13:16:12 字數 595 閱讀 3585

思路還是很簡單的,就是 「左傾」還是「右傾」的問題。需要注意的細節就是 當要查詢的值在 頭和尾的情況。

解決辦法就是在 while 語句的條件的中取 < 還是 <= 。

這個需要思考一哈,哈哈哈。

附上我的兩個**,第乙個是我初學,第二個是我學了兩個月後。

//二分法的本質就是不停縮小範圍,因為是順序的序列,於是靠找端點縮小範圍成為可能 

intbinarysearch

( list l, elementtype x)

if( x < l->data[center])if

( x == l->data[center])}

if( flag ==0)

return notfound;

}

二個月後的**,嘻嘻

position binarysearch

( list l, elementtype x )

else

if( l->data[mid]

> x )

else

}return notfound;

}

01 複雜度3 二分查詢

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

01 複雜度3 二分查詢

本題要求實現二分查詢演算法。position binarysearch list l,elementtype x 其中list結構定義如下 typedef int position typedef struct lnode list struct lnode l是使用者傳入的乙個線性表,其中elem...

01 複雜度3 二分查詢

本題要求實現二分查詢演算法。函式介面定義 position binarysearch list l,elementtype x 其中list結構定義如下 typedef int position typedef struct lnode list struct lnode l是使用者傳入的乙個線性表...