Review the key concepts, formulae, and examples before starting your quiz.
🔑Concepts
Algorithm Efficiency: This refers to the amount of resources (time and memory) that an algorithm uses. Improving an algorithm means reducing the number of steps () or the memory used.
Time Complexity: A measure of the number of operations performed by an algorithm as a function of the input size . Common complexities include (linear) and (logarithmic).
Linear Search: An algorithm that checks every element in a list until the target is found. In the worst case, it takes steps for a list of size .
Binary Search: A much faster algorithm for sorted lists. It repeatedly divides the search interval in half. It takes approximately steps.
Bubble Sort Improvement: A standard bubble sort takes comparisons. We can improve it by adding a flag to check if any swaps occurred; if no swaps occur in a pass, the list is already sorted.
Nested Loops: Algorithms with nested loops (like Bubble Sort) often have a complexity of , meaning if the input size doubles, the time taken increases by four times ().
📐Formulae
💡Examples
Problem 1:
Compare the number of steps required to find a number in a sorted list of elements using Linear Search and Binary Search.
Solution:
-
For Linear Search, the maximum number of steps is equal to the number of elements .
-
For Binary Search, the maximum number of steps is . Since , then:
-
Improvement Ratio: Binary search is more than times faster in the worst case for this list size.
Explanation:
Linear search grows linearly with the size of the data, while binary search grows logarithmically, making it significantly more efficient for large datasets.
Problem 2:
Calculate the total number of comparisons needed to sort a list of elements using the standard Bubble Sort algorithm.
Solution:
The number of comparisons is given by the formula . Given :
To visualize the addition of comparisons per pass ():
Explanation:
In each pass of the Bubble Sort, the number of comparisons decreases by 1 because the largest element 'bubbles up' to its correct position.