Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added topic Searching with Linear Search and Binary Search#39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
sidanand67 wants to merge1 commit intoOmkarPathak:master
base:master
Choose a base branch
Loading
fromsidanand67:topic-searching
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletionsSearching/Binary_Search.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
#inorder to use binary search, numbers should be in sorted order
def binary_search(numbers, target):
numbers.sort()
low = 0
high = len(numbers)-1
while low <= high:
mid = low + (high-low)//2
if numbers[mid] == target:
return True
elif numbers[mid] > target:
high = mid - 1
else:
low = mid + 1
return False




if __name__ == "__main__":
numbers = [10,30,59,32,12,9,100]
target = 9
result = binary_search(numbers, target)
if result == True:
print(f'Target number, {target} is found.')
else:
print(f'Target number, {target} is not found.')
17 changes: 17 additions & 0 deletionsSearching/Linear_Search.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
def linear_search(numbers, target):
for i in range(len(numbers)):
if numbers[i] == target:
return True
return False




if __name__ == "__main__":
numbers = [10,30,59,32,12,9,100]
target = 9
result = linear_search(numbers, target)
if result == True:
print(f'Target number, {target} is found.')
else:
print(f'Target number, {target} is not found.')

[8]ページ先頭

©2009-2025 Movatter.jp