Draw This - beginner
Draw a flowchart for performing a linear search to find the number 7 in the list [3, 5, 7, 9, 11].
Jump into concepts and practice - no test required
Draw a flowchart for performing a linear search to find the number 7 in the list [3, 5, 7, 9, 11].
+---------------------+
| Start |
+----------+----------+
|
v
+---------------------+
| Set index = 0 |
+----------+----------+
|
v
+---------------------+
| Is index < length? |----No----->+---------------------+
+----------+----------+ | Output: Not found |
| +----------+----------+
Yes |
| v
v +---------------------+
+---------------------+ | End |
| Is list[index] = 7? |--Yes---->+---------------------+
| Output: Found at index |
+----------+----------+
|
No
|
v
+---------------------+
| index = index + 1 |
+----------+----------+
|
v
(loop back to check index < length)This flowchart shows how linear search works step-by-step:
This method checks each item one by one until it finds the target or finishes the list.
linear search?target in arr?def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
arr = [2, 4, 6, 8, 10]
print(binary_search(arr, 6))def binary_search(arr, target):
low, high = 0, len(arr)
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1