Review the key concepts, formulae, and examples before starting your quiz.
🔑Concepts
An algorithm is a step-by-step procedure to solve a problem in a finite number of steps with a definite starting and ending point.
Data Structures are systematic ways of organizing and storing data so that it can be used efficiently (e.g., Arrays, Lists, Stacks, Queues).
Time Complexity refers to the amount of time an algorithm takes to run as a function of the input size .
Space Complexity refers to the amount of memory space required by the algorithm in relation to the input size .
Linear Search: A process that checks every element in a list sequentially until the target value is found or the list ends. Its worst-case complexity is .
Binary Search: An efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item. Its complexity is .
Bubble Sort: A simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. Its complexity is due to nested loops.
Big O Notation is used to describe the upper bound of the execution time or space requirements of an algorithm.
📐Formulae
💡Examples
Problem 1:
A sorted list contains elements. Determine the maximum number of comparisons required to find a specific element using Binary Search.
Solution:
In Binary Search, the maximum number of comparisons is given by the formula . Given , we find such that . Since , we have: Therefore, the maximum number of comparisons is .
Explanation:
Binary search halves the search space in each step. Since can be divided by exactly times to reach , it takes steps.
Problem 2:
Calculate the total number of comparisons made in a Bubble Sort algorithm for an array of elements in the worst-case scenario.
Solution:
The number of comparisons in Bubble Sort for elements is calculated as: Substituting :
Explanation:
In the first pass, comparisons are made; in the second, , and so on, until . The sum is .
Problem 3:
Compare the time complexity of two algorithms where Algorithm A takes steps and Algorithm B takes steps. At what value of does Algorithm A become more efficient than Algorithm B?
Solution:
Algorithm A is more efficient than Algorithm B when . Dividing both sides by (assuming ): So, for all , Algorithm A is more efficient.
Explanation:
Even though Algorithm A has a high constant factor (), its linear growth eventually becomes smaller than the quadratic growth of Algorithm B as increases.