Sorting Algorithms
- 22Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary difference in requirements between linear search and binary search algorithms?
22 questions
- 1
What is the primary difference in requirements between linear search and binary search algorithms?
Model answer
Binary search requires the list to be sorted, whereas linear search can be performed on both sorted and unsorted lists.
Explanation
Linear search simply checks each element sequentially, so the order of elements doesn't matter. Binary search, however, works by repeatedly dividing the search interval in half. This process relies fundamentally on the elements being in sorted order to correctly narrow down the search space.
- 2
Compare the worst-case time complexity of linear search and binary search.
Model answer
The worst-case time complexity for linear search is θ(n), while for binary search, it is θ(logn).
Explanation
Linear search, in the worst case, may have to examine every element in the list to find the target or confirm its absence, leading to θ(n) complexity. Binary search, by halving the search space in each step, reduces the number of comparisons much more rapidly, resulting in a θ(logn) complexity.
- 3
Under what conditions does it make sense to use binary search despite the initial cost of sorting?
Model answer
It makes sense to use binary search when performing many searches on the same dataset, as the one-time cost of sorting can be amortized over multiple searches.
Explanation
If only a single search is to be performed, the time taken to sort the list (which is at least θ(nlogn) for efficient sorts) plus the binary search time (θ(logn)) will likely be greater than or equal to a simple linear search (θ(n)). However, for k searches, the total time for sorting once and k binary searches becomes Tsort+k⋅Tbinarysearch, which is often much faster than k⋅Tlinearsearch when k is large.
- 4
Why is it impossible to sort a list in less than θ(n) time?
Model answer
It is impossible because, at minimum, every element in the list must be examined at least once to confirm its sorted position.
Explanation
To guarantee that a list is sorted, each element's value and its position relative to others must be known. This inherently requires 'touching' or 'looking at' every single element in the list at least once. Therefore, any sorting algorithm must perform at least n operations, making θ(n) the absolute lower bound for examining all elements.
- 5
Describe the core principle of Bogo Sort.
Model answer
Bogo Sort uses randomness to sort a list by repeatedly shuffling its elements until they happen to be in sorted order.
Explanation
The algorithm's name, 'bogo sort,' comes from 'bogus' or 'bizarre.' It relies on pure chance: the list is randomly reordered, and then checked for sortedness. If not sorted, it's shuffled again. This process continues until a sorted permutation is achieved by luck.
- 6
What is the worst-case time complexity of Bogo Sort, and why?
Model answer
The worst-case time complexity of Bogo Sort is unbounded, or infinite, because there's no guarantee that a random shuffle will ever result in a sorted list.
Explanation
While there's a theoretical probability of eventually sorting the list, in the worst-case scenario, the algorithm could keep shuffling indefinitely without ever hitting the correct sorted permutation. This makes its upper bound effectively infinite, as it relies purely on chance.
- 7
What is the best-case time complexity of Bogo Sort?
Model answer
The best-case time complexity of Bogo Sort is θ(n), occurring when the input list is already sorted.
Explanation
In the best case, if the input list is already sorted, Bogo Sort will perform one check for sortedness (which takes θ(n) time to examine all elements) and immediately terminate, as no shuffling is needed.
- 8
Explain the mechanism of Bubble Sort.
Model answer
Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order, effectively 'bubbling up' the largest elements to the end.
Explanation
The algorithm performs multiple passes. In each pass, it iterates through the unsorted portion of the list, comparing each pair of adjacent elements. If an element is larger than its neighbor, they are swapped. This causes the largest unsorted element to 'bubble' to its correct position at the end of the unsorted portion of the list with each pass.
- 9
Why does Bubble Sort need to repeat passes even after performing a swap in the previous pass?
Model answer
Bubble Sort needs to repeat passes because a swap might disarrange elements that were already in a relatively correct order, requiring further checks.
Explanation
When two elements are swapped, it only guarantees that those two specific elements are in their correct relative order. The movement of an element can affect the sortedness of other pairs. Therefore, if any swap occurs in a pass, it indicates that the list is not fully sorted, and another pass is necessary to ensure all elements are in their final positions.
- 10
What is the worst-case time complexity of Bubble Sort and why?
Model answer
The worst-case time complexity of Bubble Sort is θ(n2) because, in the worst case, both the inner loop and the outer loop iterate approximately n times.
Explanation
In the worst-case scenario (e.g., a reverse-sorted list), the largest element has to 'bubble' all the way to the end in the first pass, the second largest in the second pass, and so on. This means the outer loop (for passes) will run n times, and the inner loop (for comparisons and swaps) will also run approximately n times for each pass, leading to n×n=n2 operations.
- 11
Describe the process of Selection Sort.
Model answer
Selection Sort works by repeatedly finding the minimum element from the unsorted part of the list and putting it at the beginning of the unsorted part.
Explanation
In each pass, Selection Sort scans the entire unsorted portion of the list to locate the smallest element. Once the smallest element is found, it is swapped with the element at the current position (the beginning of the unsorted part). This effectively 'selects' the next smallest element and places it into its final sorted position.
- 12
How does Selection Sort differ from Bubble Sort in its approach to placing elements?
Model answer
Selection Sort places one element in its final sorted position with each pass, while Bubble Sort gradually bubbles larger elements to their correct positions over multiple passes.
Explanation
In Selection Sort, after the first pass, the smallest element is guaranteed to be at index 0. After the second pass, the second smallest element is guaranteed to be at index 1, and so on. In contrast, Bubble Sort ensures that the largest unsorted element is at its correct position at the end of the unsorted portion in each pass, but previous elements may still be out of order relative to each other.
- 13
What is the worst-case time complexity of Selection Sort and why?
Model answer
The worst-case time complexity of Selection Sort is θ(n2) because it performs n passes, and each pass involves comparing approximately n elements.
Explanation
The outer loop runs n−1 times (for each position from 0 to n−2). The inner loop, which finds the minimum element, makes n−1 comparisons in the first pass, n−2 in the second, and so on, down to 1 comparison. The sum of these comparisons is 1+2+⋯+(n−1), which is approximately 2n(n−1), resulting in θ(n2) complexity.
- 14
Why does a variation of Selection Sort that only performs one swap per pass still have a θ(n2) complexity?
Model answer
The variation still has θ(n2) complexity because the number of comparisons remains the same, even if the number of swaps is reduced.
Explanation
While reducing the number of swaps might offer a slight constant-factor speedup in practice, the dominant operation in Selection Sort, in terms of complexity, is the comparison. Each pass still requires iterating through the unsorted portion of the list to find the minimum element, which involves a linear number of comparisons. Since the number of comparisons scales quadratically with n, the overall time complexity remains θ(n2).
- 15
What is the main strategy used by Merge Sort that differentiates it from Bubble Sort and Selection Sort?
Model answer
Merge Sort uses a 'divide and conquer' strategy, recursively splitting the list in half until individual elements are reached, then merging them back in sorted order.
Explanation
Unlike Bubble Sort and Selection Sort, which use iterative comparison and swapping within a single list, Merge Sort employs recursion. It breaks down the problem into smaller, identical subproblems (sorting sublists), solves them (base case of a single-element list is inherently sorted), and then combines their solutions (merging sorted sublists) to solve the original problem.
- 16
Explain the 'merge' step in Merge Sort, assuming two sorted sublists.
Model answer
The merge step compares the first elements of two already sorted sublists, picks the smaller one to add to a new combined list, and advances the pointer for the list from which the element was taken, repeating until both sublists are exhausted.
Explanation
Because both sublists are already sorted, the smallest element overall must be the smallest element of either sublist's current front. By repeatedly selecting the smaller of the two front elements and adding it to a new list, and then moving to the next element in the chosen sublist, the algorithm can efficiently combine two sorted lists into one larger sorted list in a single pass over the elements.
- 17
What is the time complexity of the merge step in Merge Sort, and why?
Model answer
The time complexity of the merge step is θ(n) because each element from the two sublists is examined and placed into the final merged list exactly once.
Explanation
When merging two sublists that together contain n elements, the algorithm iterates through both lists, comparing and moving elements to the result list. Each comparison or element placement reduces the number of elements remaining to be processed by one. Thus, the total number of operations is directly proportional to the total number of elements being merged, resulting in linear time complexity.
- 18
How many levels of division are there in Merge Sort for a list of n elements?
Model answer
There are approximately logn levels of division in Merge Sort.
Explanation
Merge Sort repeatedly divides the list in half. If you start with n elements, the first division yields two lists of n/2 elements, the second yields four lists of n/4 elements, and so on. This process continues until lists of size 1 (the base case) are reached. The number of times you can divide n by 2 until it reaches 1 is given by log2n.
- 19
What is the overall time complexity of Merge Sort, and what makes it efficient?
Model answer
The overall time complexity of Merge Sort is θ(nlogn), which is achieved by combining the logn levels of division/merging with the θ(n) work done at each level.
Explanation
Merge Sort is efficient because it performs θ(n) work at each of its logn levels. Each level involves merging sublists, and the total work for all merges at any given level is linear (θ(n)), as each element is processed once. Since there are logn such levels, the total complexity becomes θ(nlogn). This logarithmic factor makes it significantly faster than quadratic sorts for large n.
- 20
Why is θ(nlogn) considered the fastest possible time complexity for comparison-based sorting algorithms?
Model answer
θ(nlogn) is the theoretical lower bound for comparison-based sorting algorithms because any such algorithm must perform at least this many comparisons in the worst case to determine the correct order of n distinct elements.
Explanation
This lower bound is derived from decision tree analysis. To sort n distinct items, there are n! possible permutations. Each comparison can at best halve the number of remaining possible permutations. Therefore, the minimum number of comparisons needed in the worst case to distinguish between all n! permutations is log2(n!), which simplifies to θ(nlogn) using Stirling's approximation. Algorithms like Merge Sort achieve this lower bound.
- 21
How does recursion play a role in the implementation of Merge Sort?
Model answer
Recursion in Merge Sort is used to repeatedly divide the list into smaller sublists until a base case of a single-element list is reached, then the merged results are returned up the call stack.
Explanation
The
merge_sortfunction calls itself for the left half and the right half of the list. This continues until a sublist has one or zero elements, which is the base case (an inherently sorted list). Once the base case is hit, the function returns, and themergeoperation combines the sorted sublists from the bottom up, with each recursive call returning a larger sorted list. - 22
What is the base case for the recursive
merge_sortfunction?Model answer
The base case for the
merge_sortfunction is when the input list is empty or contains only one element, as such a list is inherently sorted.Explanation
If a list has zero or one element, there are no comparisons or sorting operations needed; it's already in sorted order. This serves as the stopping condition for the recursion, preventing infinite loops and allowing the merge operations to begin combining these trivially sorted lists.

Ask anything
We've read this whole study set, so questions about it get the best answers.