forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_occurrence.py
More file actions
44 lines (35 loc) · 1.04 KB
/
first_occurrence.py
File metadata and controls
44 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
First Occurrence
Find the index of the first occurrence of a target value in a sorted array
using binary search.
Reference: https://en.wikipedia.org/wiki/Binary_search_algorithm
Complexity:
Time: O(1) best / O(log n) average / O(log n) worst
Space: O(1)
"""
from __future__ import annotations
def first_occurrence(array: list[int], query: int) -> int:
"""Find the index of the first occurrence of *query* in *array*.
Args:
array: Sorted list of integers in ascending order.
query: Value to search for.
Returns:
Index of the first occurrence of *query*, or -1 if not found.
Examples:
>>> first_occurrence([1, 2, 2, 2, 3, 4], 2)
1
>>> first_occurrence([1, 2, 3, 4, 5], 6)
-1
"""
low, high = 0, len(array) - 1
while low <= high:
mid = low + (high - low) // 2
if low == high:
break
if array[mid] < query:
low = mid + 1
else:
high = mid
if array[low] == query:
return low
return -1