1. Home
  2. Explore
  3. Computer Science
  4. Sorting Algorithms

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Sorting Algorithms

  • 19Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Search Algorithm Basics

What is the primary condition required for the binary search algorithm to function correctly and efficiently?

19 questions

  1. 1

    What is the primary condition required for the binary search algorithm to function correctly and efficiently?

    1. A

      The list must contain only unique numerical elements.

    2. B

      The list must be pre-sorted in either ascending or descending order.

    3. C

      The list needs to be stored in a linked list data structure.

    4. D

      The list should be relatively small in size to optimize performance.

    Correct answer
    B

    The list must be pre-sorted in either ascending or descending order.

    Explanation

    Binary search critically relies on the list being sorted. If the list is not sorted, binary search will produce incorrect results because its mechanism of halving the search space depends on the ordered nature of the data.

  2. 2

    Which searching algorithm exhibits a worst-case time complexity of Θ(n)\Theta(n)Θ(n) for finding an element in an unsorted list?

    1. A

      The recursive merge sort algorithm applied to searching tasks.

    2. B

      The optimized binary search algorithm for very large datasets.

    3. C

      A standard linear search performed on an unordered collection.

    4. D

      An advanced hash table search with collision resolution.

    Correct answer
    C

    A standard linear search performed on an unordered collection.

    Explanation

    Linear search, when applied to an unsorted list, must potentially check every element in the worst-case scenario (e.g., the element is at the end or not present), leading to a time complexity of Θ(n)\Theta(n)Θ(n).

  3. 3

    The time complexity for binary search on a sorted list is significantly faster than linear search. What is its time complexity?

    1. A

      It performs searches with a time complexity of Θ(log⁡n)\Theta(\log n)Θ(logn).

    2. B

      Its efficiency is best described as linear, Θ(n)\Theta(n)Θ(n).

    3. C

      It runs in constant time, noted as Θ(1)\Theta(1)Θ(1), for all cases.

    4. D

      It operates with an average time complexity of Θ(n2)\Theta(n^2)Θ(n2).

    Correct answer
    A

    It performs searches with a time complexity of Θ(log⁡n)\Theta(\log n)Θ(logn).

    Explanation

    Binary search repeatedly halves the search interval, resulting in a logarithmic time complexity of Θ(log⁡n)\Theta(\log n)Θ(logn). This is considerably faster than linear search's Θ(n)\Theta(n)Θ(n) for large nnn.

  4. 4

    When does it make sense to sort a list first to use binary search over repeatedly using linear search?

    1. A

      When many searches are performed on the same list, amortizing sort cost.

    2. B

      When the list is only searched once, minimizing initial setup cost.

    3. C

      When the list contains mostly duplicate elements, speeding up comparison.

    4. D

      When the list is very small, making the sort overhead negligible.

    Correct answer
    A

    When many searches are performed on the same list, amortizing sort cost.

    Explanation

    It makes sense to sort a list and then use binary search if many searches are to be performed. The one-time cost of sorting can be amortized over numerous faster binary searches, making the overall process more efficient than many linear searches.

  5. 5

    Why is it impossible to sort a list in less than Θ(n)\Theta(n)Θ(n) time in the best-case scenario?

    1. A

      Every element must be examined at least once to confirm its position.

    2. B

      Sorting algorithms must always include a logarithmic comparison step.

    3. C

      The memory allocation for sorting requires at least Θ(n)\Theta(n)Θ(n) operations.

    4. D

      Mathematical proof states sorting cannot be faster than quadratic time.

    Correct answer
    A

    Every element must be examined at least once to confirm its position.

    Explanation

    Even in the best-case scenario, every element in the list must be examined at least once to determine its correct position or to confirm that the list is already sorted. This fundamental requirement ensures a lower bound of Θ(n)\Theta(n)Θ(n) for sorting.

  6. 6

    Which sorting algorithm is characterized by repeatedly shuffling a list randomly until it happens to be sorted?

    1. A

      The straightforward and often used insertion sort method.

    2. B

      The highly efficient and stable merge sort algorithm.

    3. C

      The inefficient and largely impractical bogo sort algorithm.

    4. D

      The comparison-based selection sort technique.

    Correct answer
    C

    The inefficient and largely impractical bogo sort algorithm.

    Explanation

    Bogo sort (also known as random sort or monkey sort) operates by randomly reordering elements and checking for sortedness. This approach is highly inefficient and serves as an example of a 'bad' sorting algorithm.

  7. 7

    What is the worst-case time complexity of the bogo sort algorithm?

    1. A

      It consistently sorts lists in polynomial time, Θ(n3)\Theta(n^3)Θ(n3).

    2. B

      It achieves a best-case time of Θ(log⁡n)\Theta(\log n)Θ(logn) due to randomness.

    3. C

      The algorithm always completes within Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn) time.

    4. D

      Its worst-case time complexity is unbounded, approaching infinity.

    Correct answer
    D

    Its worst-case time complexity is unbounded, approaching infinity.

    Explanation

    In the worst-case scenario, bogo sort could theoretically never sort the list, or take an extraordinarily long time due to its reliance on pure chance. Thus, its worst-case time complexity is unbounded, or infinite.

  8. 8

    What is the best-case time complexity for bogo sort, assuming the input list is already sorted?

    1. A

      The best-case time complexity is Θ(n)\Theta(n)Θ(n).

    2. B

      The optimal scenario is Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn) due to initial checks.

    3. C

      It is always Θ(1)\Theta(1)Θ(1) because no sorting is actually needed.

    4. D

      The best-case scenario for bogo sort is Θ(n2)\Theta(n^2)Θ(n2).

    Correct answer
    A

    The best-case time complexity is Θ(n)\Theta(n)Θ(n).

    Explanation

    If the input list is already sorted, bogo sort will perform one check to confirm sortedness, which requires examining each element once. This results in a best-case time complexity of Θ(n)\Theta(n)Θ(n).

  9. 9

    Which sorting algorithm repeatedly compares adjacent elements and swaps them if they are in the wrong order, causing the largest elements to 'bubble up'?

    1. A

      The bubble sort algorithm, frequently criticized for its inefficiency.

    2. B

      The shell sort algorithm, an improvement over insertion sort methods.

    3. C

      The quicksort algorithm, known for its average case efficiency.

    4. D

      The heap sort algorithm, using a binary tree structure for sorting.

    Correct answer
    A

    The bubble sort algorithm, frequently criticized for its inefficiency.

    Explanation

    Bubble sort works by repeatedly stepping through the list, comparing adjacent elements and swapping them if they are in the wrong order. This process causes larger elements to 'bubble' to their correct positions at the end of the list.

  10. 10

    What is the worst-case time complexity of the bubble sort algorithm?

    1. A

      The worst-case time complexity is Θ(n2)\Theta(n^2)Θ(n2).

    2. B

      It consistently performs in Θ(n)\Theta(n)Θ(n) time even for inverted lists.

    3. C

      The worst-case scenario for bubble sort is Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn).

    4. D

      It's Θ(1)\Theta(1)Θ(1) when the list is perfectly sorted in reverse.

    Correct answer
    A

    The worst-case time complexity is Θ(n2)\Theta(n^2)Θ(n2).

    Explanation

    In the worst-case scenario (e.g., a reverse-sorted list), bubble sort requires nnn passes, and each pass involves up to nnn comparisons and swaps. This leads to a quadratic time complexity of Θ(n2)\Theta(n^2)Θ(n2).

  11. 11

    After the first pass of bubble sort on a list of nnn elements, what can be guaranteed about the list's order?

    1. A

      All elements in the first half of the list are correctly sorted.

    2. B

      The list is now entirely sorted, requiring no further passes.

    3. C

      The largest element has moved to its correct position at the end.

    4. D

      The smallest element has moved to the beginning of the list.

    Correct answer
    C

    The largest element has moved to its correct position at the end.

    Explanation

    In bubble sort, during each full pass, the largest unsorted element 'bubbles up' to its correct final position at the end of the unsorted portion of the list. After the first pass, the very largest element is guaranteed to be in its correct place.

  12. 12

    Which sorting algorithm improves upon bubble sort by finding the smallest element in the unsorted portion and placing it at the correct beginning position?

    1. A

      The merge sort algorithm, which uses a divide and conquer strategy.

    2. B

      The selection sort algorithm, known for its consistent swaps.

    3. C

      The quicksort algorithm, employing partitioning for efficiency.

    4. D

      The insertion sort algorithm, building the final sorted array one item at a time.

    Correct answer
    B

    The selection sort algorithm, known for its consistent swaps.

    Explanation

    Selection sort works by repeatedly finding the minimum element from the unsorted part of the list and putting it at the beginning. This directly places elements into their final sorted positions, making it an improvement over bubble sort's repeated swaps.

  13. 13

    What is the worst-case time complexity of the selection sort algorithm?

    1. A

      Its worst-case performance is bounded by Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn).

    2. B

      It operates in nearly constant time Θ(1)\Theta(1)Θ(1) for small inputs.

    3. C

      It always achieves a linear time complexity of Θ(n)\Theta(n)Θ(n).

    4. D

      The algorithm's worst-case time complexity is Θ(n2)\Theta(n^2)Θ(n2).

    Correct answer
    D

    The algorithm's worst-case time complexity is Θ(n2)\Theta(n^2)Θ(n2).

    Explanation

    Selection sort involves nested loops: the outer loop iterates nnn times (for each position to fill), and the inner loop iterates about nnn times (to find the minimum element). This results in a quadratic time complexity of Θ(n2)\Theta(n^2)Θ(n2).

  14. 14

    How does the optimized selection sort variation (doing one swap per pass) impact its asymptotic time complexity?

    1. A

      It drastically reduces complexity to Θ(log⁡n)\Theta(\log n)Θ(logn) by minimizing swaps.

    2. B

      It changes the complexity to Θ(n)\Theta(n)Θ(n) by improving inner loop operations.

    3. C

      It does not change the asymptotic complexity; it remains Θ(n2)\Theta(n^2)Θ(n2).

    4. D

      It transforms the algorithm into a Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn) sorting method.

    Correct answer
    C

    It does not change the asymptotic complexity; it remains Θ(n2)\Theta(n^2)Θ(n2).

    Explanation

    While performing only one swap per pass instead of multiple can reduce the constant factor (number of actual swap operations), it does not change the fundamental number of comparisons needed. The nested loops still result in Θ(n2)\Theta(n^2)Θ(n2) comparisons, thus the asymptotic time complexity remains Θ(n2)\Theta(n^2)Θ(n2).

  15. 15

    Which sorting algorithm employs a 'divide and conquer' strategy by recursively splitting the list and then merging the sorted sublists?

    1. A

      The merge sort algorithm, using recursive division and merging.

    2. B

      The bogo sort, relying on random permutations for sorting.

    3. C

      The simple bubble sort, known for its step-by-step comparisons.

    4. D

      The selection sort, which iteratively finds the smallest element.

    Correct answer
    A

    The merge sort algorithm, using recursive division and merging.

    Explanation

    Merge sort is a classic 'divide and conquer' algorithm. It recursively divides the list into two halves until it reaches base cases of single-element lists, and then merges those sorted sublists back together to form a fully sorted list.

  16. 16

    What is the base case for the recursive merge sort algorithm?

    1. A

      When the list size exceeds a certain threshold, switching to linear sort.

    2. B

      When the list is empty or contains only a single element, which is by definition sorted.

    3. C

      When the list is found to be perfectly sorted during a preliminary check.

    4. D

      When the list contains only two elements and they are swapped if necessary.

    Correct answer
    B

    When the list is empty or contains only a single element, which is by definition sorted.

    Explanation

    The base case for merge sort's recursion is when a sublist is empty or contains only one element. A list with zero or one element is inherently considered sorted, allowing the merging process to begin.

  17. 17

    What is the time complexity of the merge step in merge sort, assuming two already sorted sublists?

    1. A

      It operates in Θ(log⁡n)\Theta(\log n)Θ(logn) time by binary comparison.

    2. B

      It takes Θ(n)\Theta(n)Θ(n) time because each element is examined once.

    3. C

      It requires Θ(n2)\Theta(n^2)Θ(n2) operations due to nested loops.

    4. D

      It's a constant time operation, Θ(1)\Theta(1)Θ(1), for any list size.

    Correct answer
    B

    It takes Θ(n)\Theta(n)Θ(n) time because each element is examined once.

    Explanation

    The merge step efficiently combines two sorted sublists into one. It works by comparing the first elements of each list and adding the smaller one to the result, then advancing that list's pointer. Each element is processed exactly once, resulting in a linear time complexity of Θ(n)\Theta(n)Θ(n).

  18. 18

    How many levels of recursion are involved in merge sort for a list of nnn elements, and what is its Big-Theta complexity?

    1. A

      There are nnn levels, leading to a Θ(n2)\Theta(n^2)Θ(n2) complexity.

    2. B

      There are log⁡n\log nlogn levels, resulting in a Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn) complexity.

    3. C

      There are 2n2^n2n levels, making it an exponential Θ(2n)\Theta(2^n)Θ(2n) complexity.

    4. D

      There is only one level, ensuring Θ(n)\Theta(n)Θ(n) overall complexity.

    Correct answer
    B

    There are log⁡n\log nlogn levels, resulting in a Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn) complexity.

    Explanation

    Merge sort repeatedly divides the list in half, similar to binary search, which results in log⁡n\log nlogn levels of recursion. Since each level involves a total of Θ(n)\Theta(n)Θ(n) work (for the merge step), the overall time complexity is Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn).

  19. 19

    What is considered the fastest possible asymptotic time complexity for any comparison-based sorting algorithm?

    1. A

      Algorithms can reach a constant time complexity, Θ(1)\Theta(1)Θ(1).

    2. B

      The theoretical lower bound is Θ(n2)\Theta(n^2)Θ(n2).

    3. C

      It is possible to sort in linear time, Θ(n)\Theta(n)Θ(n), under all conditions.

    4. D

      The optimal efficiency achieved is Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn).

    Correct answer
    D

    The optimal efficiency achieved is Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn).

    Explanation

    For comparison-based sorting algorithms, the theoretical lower bound for time complexity is Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn). This means no comparison sort can perform asymptotically faster than this, making merge sort (and quicksort, heapsort) optimal in this class.

Question 1 of 19
Question 1 of 19

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent