feat(patterns): pattern taxonomy + is_optimal
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
name: Binary Search
|
||||
slug: binary-search
|
||||
difficulty_level: 2
|
||||
pattern_type: algorithm
|
||||
display_order: 4
|
||||
|
||||
description: >
|
||||
Efficiently search sorted data by repeatedly dividing the search space in half.
|
||||
@@ -200,3 +202,172 @@ related_patterns:
|
||||
- two-pointers
|
||||
|
||||
prerequisite_patterns: []
|
||||
|
||||
visualization_examples:
|
||||
- id: binary-search-find-target
|
||||
title: Find Target in Sorted Array
|
||||
input:
|
||||
nums: [1, 3, 5, 7, 9, 11, 13]
|
||||
target: 9
|
||||
code: |
|
||||
def binary_search(nums, target):
|
||||
left, right = 0, len(nums) - 1
|
||||
|
||||
while left <= right:
|
||||
mid = left + (right - left) // 2
|
||||
|
||||
if nums[mid] == target:
|
||||
return mid
|
||||
elif nums[mid] < target:
|
||||
left = mid + 1
|
||||
else:
|
||||
right = mid - 1
|
||||
|
||||
return -1
|
||||
steps:
|
||||
- id: step-1
|
||||
description: "Initialize pointers: left=0, right=6. Search space is entire array."
|
||||
structures:
|
||||
nums:
|
||||
type: array
|
||||
values:
|
||||
- { value: 1, state: default }
|
||||
- { value: 3, state: default }
|
||||
- { value: 5, state: default }
|
||||
- { value: 7, state: default }
|
||||
- { value: 9, state: default }
|
||||
- { value: 11, state: default }
|
||||
- { value: 13, state: default }
|
||||
pointers:
|
||||
left: 0
|
||||
right: 6
|
||||
variables:
|
||||
target: 9
|
||||
left: 0
|
||||
right: 6
|
||||
codeHighlight:
|
||||
startLine: 2
|
||||
endLine: 2
|
||||
|
||||
- id: step-2
|
||||
description: "Calculate mid=3. nums[3]=7 < target=9, so search right half."
|
||||
structures:
|
||||
nums:
|
||||
type: array
|
||||
values:
|
||||
- { value: 1, state: visited }
|
||||
- { value: 3, state: visited }
|
||||
- { value: 5, state: visited }
|
||||
- { value: 7, state: comparing }
|
||||
- { value: 9, state: default }
|
||||
- { value: 11, state: default }
|
||||
- { value: 13, state: default }
|
||||
pointers:
|
||||
left: 0
|
||||
mid: 3
|
||||
right: 6
|
||||
variables:
|
||||
target: 9
|
||||
left: 0
|
||||
right: 6
|
||||
mid: 3
|
||||
codeHighlight:
|
||||
startLine: 9
|
||||
endLine: 10
|
||||
|
||||
- id: step-3
|
||||
description: "Update left=4. New search space is indices 4-6."
|
||||
structures:
|
||||
nums:
|
||||
type: array
|
||||
values:
|
||||
- { value: 1, state: visited }
|
||||
- { value: 3, state: visited }
|
||||
- { value: 5, state: visited }
|
||||
- { value: 7, state: visited }
|
||||
- { value: 9, state: default }
|
||||
- { value: 11, state: default }
|
||||
- { value: 13, state: default }
|
||||
pointers:
|
||||
left: 4
|
||||
right: 6
|
||||
variables:
|
||||
target: 9
|
||||
left: 4
|
||||
right: 6
|
||||
codeHighlight:
|
||||
startLine: 4
|
||||
endLine: 4
|
||||
|
||||
- id: step-4
|
||||
description: "Calculate mid=5. nums[5]=11 > target=9, so search left half."
|
||||
structures:
|
||||
nums:
|
||||
type: array
|
||||
values:
|
||||
- { value: 1, state: visited }
|
||||
- { value: 3, state: visited }
|
||||
- { value: 5, state: visited }
|
||||
- { value: 7, state: visited }
|
||||
- { value: 9, state: default }
|
||||
- { value: 11, state: comparing }
|
||||
- { value: 13, state: visited }
|
||||
pointers:
|
||||
left: 4
|
||||
mid: 5
|
||||
right: 6
|
||||
variables:
|
||||
target: 9
|
||||
left: 4
|
||||
right: 6
|
||||
mid: 5
|
||||
codeHighlight:
|
||||
startLine: 11
|
||||
endLine: 12
|
||||
|
||||
- id: step-5
|
||||
description: "Update right=4. Search space narrowed to index 4 only."
|
||||
structures:
|
||||
nums:
|
||||
type: array
|
||||
values:
|
||||
- { value: 1, state: visited }
|
||||
- { value: 3, state: visited }
|
||||
- { value: 5, state: visited }
|
||||
- { value: 7, state: visited }
|
||||
- { value: 9, state: active }
|
||||
- { value: 11, state: visited }
|
||||
- { value: 13, state: visited }
|
||||
pointers:
|
||||
left: 4
|
||||
right: 4
|
||||
variables:
|
||||
target: 9
|
||||
left: 4
|
||||
right: 4
|
||||
codeHighlight:
|
||||
startLine: 4
|
||||
endLine: 4
|
||||
|
||||
- id: step-6
|
||||
description: "Calculate mid=4. nums[4]=9 == target=9. Found at index 4!"
|
||||
structures:
|
||||
nums:
|
||||
type: array
|
||||
values:
|
||||
- { value: 1, state: visited }
|
||||
- { value: 3, state: visited }
|
||||
- { value: 5, state: visited }
|
||||
- { value: 7, state: visited }
|
||||
- { value: 9, state: found }
|
||||
- { value: 11, state: visited }
|
||||
- { value: 13, state: visited }
|
||||
pointers:
|
||||
mid: 4
|
||||
variables:
|
||||
target: 9
|
||||
mid: 4
|
||||
result: 4
|
||||
codeHighlight:
|
||||
startLine: 7
|
||||
endLine: 8
|
||||
|
||||
Reference in New Issue
Block a user