More Related Content
More from joonjhokil (19)
Binary search
- 2. Binary Search O (log n)
.
balanced binary search tree .
22
105
31
150
24
15
5
1
19
17
Find 31
- 3. Binary Search .
- Index
(RandomAccessCollection )
- .
RandomAccessCollection ?
O(1) Index
.
- 4. 1 5 15 17 19 22 24 31 105 150
firstIndex(of:)
1 5 15 17 19 22 24 31 105 150
binarySearch(for:)
- 5. Step 1: Find middle index
1 5 15 17 19 22 24 31 105 150
Step 2: Check the element at the middle index
If (31 == 22)
index .
.
Step 3 .
- 6. Step 3: Recursively call binary Search
1 5 15 17 19 22 24 31 105 150
If (31 < 22)
1 5 15 17 19 22 24 31 105 150
else
- 7. Step 1~3
24 31 105 150
Step 2 :If (31 == 105)
Step 3 :If (31 < 105)
else
Step 1 : 105 Step 1 : 31
Step 2 :If (31 == 31)