Sorting and Searching Algorithms Flashcards
What is the time complexity of Merge Sort?
O(n log n) in all cases.
What is the worst-case complexity of Quick Sort?
O(n²), but it has an average complexity of O(n log n).
What is binary search, and what is its time complexity?
A divide-and-conquer search algorithm with O(log n) complexity.
What is the best and worst-case time complexity of linear search?
Best: O(1) (first element is the target), Worst: O(n).
What is the worst-case scenario for Quick Sort?
When the pivot is always the smallest or largest element → O(n²) complexity.
What is the best sorting algorithm for nearly sorted data?
Insertion Sort (O(n)).
What is the best-case complexity of Binary Search?
O(1) (if the middle element is the target).