Review the key concepts, formulae, and examples before starting your quiz.
🔑Concepts
An algorithm is a well-defined, step-by-step procedure to solve a specific problem in a finite number of steps.
Characteristics of a good algorithm include Finiteness, Definiteness (clear steps), Input, Output, and Effectiveness.
Analysing an algorithm involves measuring its efficiency in terms of Time Complexity and Space Complexity , where is the input size.
Time complexity represents the amount of time an algorithm takes to run, usually expressed using Big O notation .
Linear Search is an algorithm that checks every element in a list one by one until the target is found. Its worst-case time complexity is .
Binary Search is a more efficient algorithm used on sorted lists. It repeatedly divides the search interval in half. Its worst-case time complexity is .
Comparison of efficiency: For large values of , an algorithm with complexity is significantly faster than one with , and is faster than .
📐Formulae
💡Examples
Problem 1:
Compare the number of comparisons required to find a number in a sorted list of elements using Linear Search and Binary Search in the worst-case scenario.
Solution:
-
For Linear Search: In the worst case, the algorithm searches all elements. Number of comparisons = .
-
For Binary Search: In the worst case, the number of comparisons is . Since , then . Number of comparisons = .
Explanation:
Linear Search grows linearly with input size, while Binary Search grows logarithmically, making it much faster for large sorted datasets.
Problem 2:
An algorithm calculates the sum of the first numbers using a loop that iterates times. Another algorithm uses the formula . Identify their time complexities.
Solution:
-
Loop Algorithm: Since the loop runs from to , the number of operations is proportional to . Time Complexity = .
-
Formula Algorithm: The formula involves a fixed number of operations (one addition, one multiplication, and one division) regardless of the value of . Time Complexity = (Constant time).
Explanation:
The formula-based approach is more efficient because its execution time does not increase with the size of the input .