Lecture 8
Lecture 8
CS3610
03 Binary search
a = 2, b = 2, d = 0
𝑙𝑜𝑔𝑏 𝑎
a> bd, use case 3: O(n )
𝑙𝑜𝑔2 2 = 1 → O(n)
for i = 2 to n do
pow =* pow
return pow
03 Binary search
9 4 -10 -3 2 -6
.
.
F2023 Analysis, Design
. of Algorithms 10
Brute force
nonnegative
Algorithm NegBeforePos(A[0..n − 1])
i ← 0; j ← n − 1
while i < j do
if A[i] < 0 //shrink the unknown section from the left
i ← i +1
else //shrink the unknown section from the right
swap(A[i],A[j]) .
.
F2023
j←j−1 Analysis, Design
. of Algorithms 11
Agenda
03 Binary search
➢ Sequential search can be used with about the same efficiency whether a list
is implemented as an array or as a linked list. Is it also true for binary search?
(Of course, we assume that a list is sorted for binary search.)
3. Binary search