Binary Search (Recurrence Relation)

PUBLISHED: MAY 2, 20261 MIN READ

Binary SearchFloor Functionfloor(x): If x is a real number, then floor(x) is the greatest integer less than or equal to x.Ceil Functionceil(x): If x is a real n

Abhishek Singh Rajput
Abhishek SinghAuthor
shellos
#422
shellos

Binary Search

Floor Function

  • floor(x): If x is a real number, then floor(x) is the greatest integer less than or equal to x.

Ceil Function

  • ceil(x): If x is a real number, then ceil(x) is the smallest integer greater than or equal to x.

Binary Search Recursive Equation

  1. Finding the middle element:
mid=low+high2mid=\frac{low+high}{2}
  1. Comparing the key with the middle element:
    • If key == arr[mid] → element found
    • If key < arr[mid] → search in left half
    • If key > arr[mid] → search in right half

Recursive Equation

t(n)=C1+C2+t(n2)t(n)=C_1+C_2+t\left(\frac{n}{2}\right)
  • id
  • = Time to comparing key with mid
  • $t\left(\frac{2}\right)$ = Time to search in the remaining half the array

  • $dbyin

T(n)=T(n2k)+kT(n)=T\left(\frac{n}{2^k}\right)+k
T(n)=T(nn)+log2nT(n)=T\left(\frac{n}{n}\right)+\log_2n
T(n)=1+log2nT(n)=1+log_2n
T(n)=O(log2n)\boxed{T(n)=O(\log_2n)}

Recurrence Relation

  • Def a mapping from natural numme set.
    A recurrence relation defines each term of tuence as a function of itsibonacci Relation
f(0)=0,f(1)=1f(0)=0,f(1)=1
f(n)=f(n1)+f(n2)for n2f(n)=f(n-1)+f\left(n-2\right)\quad\text{for }n\geq2

This is a classic recurrence relation.

General Form of Recurrence Relation

Let Is a sequence where an is term of the sequence then a relation of type

f(n)=an+c1an1+c2an2++ckank,nkf(n)=a_{n}+c_1a_{n-1}+c_2a_{n-2}+\ldots+c_{k}a_{n-k},\quad n\geq k
  • is some functi, \dots, c_k$ are coner, called the degree ofinear recurrance relation with constant coefficient.

condition

Types of Recurrence Relations

  1. Homogeneous Linear Recurrence Relation with constant coefficient
an+C1an1+C2an2++Ckank=0nka_{n}+C_1a_{n-1}+C_2a_{n-2}+\ldots+C_{k}a_{n-k}=0\quad n\geq k
  1. Inhomogeneous Linear Recurrence Relation with constant coefficient
f(n)=an+C1an1+C2an2++Ckank0f(n)=a_{n}+C_1a_{n-1}+C_2a_{n-2}+\ldots+C_{k}a_{n-k}\neq0

Examples of Recurrence Relation

Homogeneous
Inhomogeneous
Inhomogeneous

Meaning of the Solution of a Recurrence Relation