python二分查找搜索算法的多種實(shí)現(xiàn)方法
二分查找搜索算法利用了元素集合,這些元素在一次比較后就忽略了一半的元素,從而進(jìn)行了排序。
- 將 x 與中間元素進(jìn)行比較。
- 如果 x 與中間元素匹配,則返回中間索引。
- 否則,如果 x 大于 mid 元素,則 x 只能位于 mid 元素之后的右(大)半子數(shù)組中。然后,我們?cè)俅螌⒃撍惴☉?yīng)用于右半部分。
- 否則,如果 x 較小,則目標(biāo) x 必須位于左(下)半部分。因此,我們將算法應(yīng)用于左半部分。
使用遞歸進(jìn)行二分查找搜索
# Python 3 program for recursive binary search. # Modifications needed for the older Python 2 are found in comments. # Returns index of x in arr if present, else -1 def binary_search(arr, low, high, x): # Check base case if high >= low: mid = (high + low) // 2 # If element is present at the middle itself if arr[mid] == x: return mid # If element is smaller than mid, then it can only # be present in left subarray elif arr[mid] > x: return binary_search(arr, low, mid - 1, x) # Else the element can only be present in right subarray else: return binary_search(arr, mid + 1, high, x) else: # Element is not present in the array return -1 # Test array arr = [ 2, 3, 4, 10, 40 ] x = 10 # Function call result = binary_search(arr, 0, len(arr)-1, x) if result != -1: print("Element is present at index", str(result)) else: print("Element is not present in array")
輸出
Element is present at index 3
使用迭代進(jìn)行搜索
# Iterative Binary Search Function # It returns index of x in given array arr if present, # else returns -1 def binary_search(arr, x): low = 0 high = len(arr) - 1 mid = 0 while low <= high: mid = (high + low) // 2 # If x is greater, ignore left half if arr[mid] < x: low = mid + 1 # If x is smaller, ignore right half elif arr[mid] > x: high = mid - 1 # means x is present at mid else: return mid # If we reach here, then the element was not present return -1 # Test array arr = [ 2, 3, 4, 10, 40 ] x = 10 # Function call result = binary_search(arr, x) if result != -1: print("Element is present at index", str(result)) else: print("Element is not present in array")
輸出
Element is present at index 3
使用內(nèi)置 bisect 模塊
循序漸進(jìn)的方法:
- 該代碼導(dǎo)入 bisect 模塊,該模塊提供對(duì)二進(jìn)制搜索的支持。
- 定義了 binary_search_bisect() 函數(shù),該函數(shù)將數(shù)組 arr 和搜索 x 的元素作為輸入。
- 該函數(shù)調(diào)用 bisect 模塊的 bisect_left() 函數(shù),該函數(shù)在排序數(shù)組 arr 中查找元素的位置,其中應(yīng)插入 x 以保持排序順序。如果該元素已存在于數(shù)組中,則此函數(shù)將返回其位置。
- 然后,該函數(shù)檢查返回的索引 i 是否在數(shù)組范圍內(nèi),以及該索引處的元素是否等于 x。
- 如果條件為 true,則該函數(shù)返回索引 i 作為元素在數(shù)組中的位置。
- 如果條件為 false,則該函數(shù)返回 -1,指示該元素不存在于數(shù)組中。
- 然后,代碼定義一個(gè)數(shù)組 arr 和一個(gè)要搜索的元素 x。
- 使用 arr 和 x 作為輸入調(diào)用 binary_search_bisect() 函數(shù),返回的結(jié)果存儲(chǔ)在 result 變量中。
- 然后,代碼檢查結(jié)果是否不等于 -1,指示該元素存在于數(shù)組中。如果為 true,則打印元素在數(shù)組中的位置。
- 如果結(jié)果等于 -1,則代碼將打印一條消息,指出該元素在數(shù)組中不存在。
import bisect def binary_search_bisect(arr, x): i = bisect.bisect_left(arr, x) if i != len(arr) and arr[i] == x: return i else: return -1 # Test array arr = [2, 3, 4, 10, 40] x = 10 # Function call result = binary_search_bisect(arr, x) if result != -1: print("Element is present at index", str(result)) else: print("Element is not present in array")
輸出
Element is present at index 3
到此這篇關(guān)于python二分查找搜索算法的多種實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)python二分查找搜索算法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python優(yōu)雅實(shí)現(xiàn)二分查找的示例詳解
- 使用Python實(shí)現(xiàn)二分法查找的示例
- Python實(shí)現(xiàn)二分法查找及優(yōu)化的示例詳解
- Python?二分查找之bisect庫的使用詳解
- Python算法練習(xí)之二分查找算法的實(shí)現(xiàn)
- 詳解Python查找算法的實(shí)現(xiàn)(線性,二分,分塊,插值)
- Python語言實(shí)現(xiàn)二分法查找
- python二分法查找函數(shù)底值
- python二分法查找實(shí)例代碼
- python中二分查找法的實(shí)現(xiàn)方法
- python實(shí)現(xiàn)二分查找算法
相關(guān)文章
Pyhton模塊和包相關(guān)知識(shí)總結(jié)
文中詳細(xì)整理了關(guān)于Python模塊和包的相關(guān)知識(shí)點(diǎn),剛?cè)腴TPython的小伙伴們可以學(xué)習(xí)一下,有助于加深Python基礎(chǔ)的理解.而且有詳細(xì)說明及代碼示例,需要的朋友可以參考下2021-05-05解決新版Pycharm中Matplotlib圖像不在彈出獨(dú)立的顯示窗口問題
今天小編就為大家分享一篇解決新版Pycharm中Matplotlib圖像不在彈出獨(dú)立的顯示窗口問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python實(shí)現(xiàn)獲取網(wǎng)頁內(nèi)容及自動(dòng)填表單與登錄功能
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)模擬瀏覽器啟動(dòng),獲取網(wǎng)頁內(nèi)容、自動(dòng)填表單、自動(dòng)登錄、自動(dòng)過驗(yàn)證碼等功能,需要的可以參考一下2023-03-03全面解析Python的While循環(huán)語句的使用方法
這篇文章主要介紹了全面解析Python的While循環(huán)語句的使用方法,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10本地部署Python?Flask并搭建web問答應(yīng)用程序框架實(shí)現(xiàn)遠(yuǎn)程訪問的操作方法
Flask是一個(gè)Python編寫的Web微框架,使用Python語言快速實(shí)現(xiàn)一個(gè)網(wǎng)站或Web服務(wù),本期教程我們使用Python Flask搭建一個(gè)web問答應(yīng)用程序框架,并結(jié)合cpolar內(nèi)網(wǎng)穿透工具將我們的應(yīng)用程序發(fā)布到公共網(wǎng)絡(luò)上,實(shí)現(xiàn)可多人遠(yuǎn)程進(jìn)入到該web應(yīng)用程序訪問,需要的朋友可以參考下2023-12-12解決pyCharm中 module 調(diào)用失敗的問題
今天小編就為大家分享一篇解決pyCharm中 module 調(diào)用失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02django 簡(jiǎn)單實(shí)現(xiàn)登錄驗(yàn)證給你
這篇文章主要介紹了django 簡(jiǎn)單實(shí)現(xiàn)登錄驗(yàn)證給你,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11