Fibonacci Time Complexity (Continued)

Recurrence Relation:

Time Complexity:

where:

Therefore:

where (golden ratio)

We take the larger value for upper bound


Array Search Problems

Example Array:

Task: Check if is present

Linear Search Algorithm

Given: Array with elements, search for value

for i = 0 to n-1:
    if A[i] == X:
        return i  // Found at index i
return -1  // Not found

Time Complexity:


Example: If Array sorted:

Finding 20:

  • Check mid of array
  • Since : Check left half

Binary Search Algorithm

low = 0
high = n - 1
while (low ≤ high):
    mid = (low + high) / 2
    if A[mid] == X:
        return mid  // Found
    else if A[mid] > X:
        high = mid - 1  // Search left half
    else:
        low = mid + 1   // Search right half
return -1  // Not found

Time Complexity Analysis:

where is constant time for comparison and mid calculation.

Solving the Recurrence:

Base case:

When :

Therefore:

Final Complexity:

Note: Efficient but array must be sorted!