-
Notifications
You must be signed in to change notification settings - Fork 80
Gaoping update #1
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 73 additions & 26 deletions
BinarySearch.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,86 @@ | ||
| import random | ||
| # import random | ||
| # | ||
| # | ||
| # def InsertionSort(A, n): | ||
| # for j in range(1, n): | ||
| # key = A[j] | ||
| # # Insert A[j] into the sorted sequence[1...j-1] | ||
| # i = j - 1 | ||
| # while i >= 0 and A[i] > key: | ||
| # A[i + 1] = A[i] | ||
| # i = i - 1 | ||
| # A[i + 1] = key | ||
| # return A | ||
| # | ||
| # | ||
| # def BinarySearch(A, p, r, key): | ||
| # if p >= r: | ||
| # return -1 | ||
| # q = (p + r) / 2 | ||
| # if A[q] == key: | ||
| # return q | ||
| # elif A[q] < key: | ||
| # return BinarySearch(A, q + 1, r, key) | ||
| # else: | ||
| # return BinarySearch(A, p, q, key) | ||
| # | ||
| # | ||
| # # Pre procedure. | ||
| # A = [] | ||
| # s = random.randint(5, 100) | ||
| # for i in range(0, s): | ||
| # A.append(random.randint(0, 1000)) | ||
| # A = InsertionSort(A, len(A)) | ||
| # key = random.choice(A) | ||
| # | ||
| # print "Now displaying BinarySearch." | ||
| # print A | ||
| # print key | ||
| # print BinarySearch(A, 0, len(A) - 1, key) | ||
|
|
||
| import random | ||
| from typing import List | ||
|
|
||
| def InsertionSort(A, n): | ||
| for j in range(1, n): | ||
| def insertion_sort(A: List[int]) -> List[int]: | ||
| for j in range(1, len(A)): | ||
| key = A[j] | ||
| # Insert A[j] into the sorted sequence[1...j-1] | ||
| i = j - 1 | ||
| while i >= 0 and A[i] > key: | ||
| A[i + 1] = A[i] | ||
| i = i - 1 | ||
| i -= 1 | ||
| A[i + 1] = key | ||
| return A | ||
|
|
||
| def binary_search(A: List[int], p: int, r: int, key: int) -> int: | ||
| while p <= r: | ||
| q = (p + r) // 2 | ||
| if A[q] == key: | ||
| return q | ||
| elif A[q] < key: | ||
| p = q + 1 | ||
| else: | ||
| r = q - 1 | ||
| return -1 | ||
|
|
||
| def BinarySearch(A, p, r, key): | ||
| if p >= r: | ||
| return -1 | ||
| q = (p + r) / 2 | ||
| if A[q] == key: | ||
| return q | ||
| elif A[q] < key: | ||
| return BinarySearch(A, q + 1, r, key) | ||
| else: | ||
| return BinarySearch(A, p, q, key) | ||
|
|
||
| # Generate random sorted list | ||
| A = [random.randint(0, 1000) for _ in range(random.randint(5, 100))] | ||
| A = insertion_sort(A) | ||
|
|
||
| # Pre procedure. | ||
| A = [] | ||
| s = random.randint(5, 100) | ||
| for i in range(0, s): | ||
| A.append(random.randint(0, 1000)) | ||
| A = InsertionSort(A, len(A)) | ||
| # Pick a random key from the list | ||
| key = random.choice(A) | ||
|
|
||
| print "Now displaying BinarySearch." | ||
| print A | ||
| print key | ||
| print BinarySearch(A, 0, len(A) - 1, key) | ||
| # Display results | ||
| print("Now displaying BinarySearch:") | ||
| print("Sorted list:", A) | ||
| print("Key to search:", key) | ||
| print("Key index:", binary_search(A, 0, len(A) - 1, key)) | ||
| # ✅ 优化点说明: | ||
| # 兼容 Python 3:print 改为函数形式。 | ||
| # | ||
| # 修复 BinarySearch 中浮点索引问题:q = (p + r) // 2 替代 /。 | ||
| # | ||
| # 添加类型注解和更清晰的结构。 | ||
| # | ||
| # 逻辑优化:在 BinarySearch 中使用更标准的边界处理(含等于边界时也判断)。 | ||
| # | ||
| # 生成数据清晰可控。 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.