Merge sort and quicksort exam review

This exam review covers key concepts, techniques, and comparisons of merge sort and quicksort algorithms in computer science, focusing on sorting efficiency and implementation details.

Lucas67·32 flashcards·32 questions
collegecomputer_sciencealgorithms
0
Known
1 / 32
0
Learning
Front

What is merge sort?

Tap to flip
Back

A divide-and-conquer algorithm that sorts by dividing the array into halves, sorting each half, and then merging them back together.

Tap to flip
Got it
Still learning

Quiz(32 questions)

Question 1 of 32

1. What type of algorithm is merge sort?

Terms in this Study Set(32)

Flashcards 1(16)

What is merge sort?

A divide-and-conquer algorithm that sorts by dividing the array into halves, sorting each half, and then merging them back together.

How does quicksort work?

Selects a 'pivot' element, partitions the array into elements smaller and larger than the pivot, then recursively sorts the partitions.

True or False: Merge sort is stable.

True - It maintains the relative order of equal elements during sorting.

What is the worst-case time complexity of quicksort?

O(n²) - This occurs when the pivot is the smallest or largest element repeatedly.

Fill in the blank: Merge sort is _____ when dealing with large datasets.

O(n log n) - This is its average and worst-case time complexity.

Comparison: Merge sort vs. Quicksort

- Merge sort is stable - Quicksort is generally faster - Merge sort requires additional space

What is the average-case time complexity of quicksort?

O(n log n) - This efficiency makes quicksort suitable for many practical applications.

What is a pivot in quicksort?

An element chosen to partition the array. It helps in determining the order of elements.

True or False: Merge sort requires additional memory.

True - It requires O(n) additional space for the temporary arrays used during merging.

How do you perform a merge in merge sort?

Combine two sorted arrays into a single sorted array by comparing elements one by one.

What is the best-case time complexity of quicksort?

O(n log n) - This occurs when the pivot divides the array into two equal halves.

Cause → Effect: Choosing a bad pivot in quicksort.

Causes imbalanced partitions → Leads to O(n²) worst-case performance.

What is a base case in merge sort?

An array with one element, which is already sorted and does not require further division.

What does in-place mean in quicksort?

Quicksort sorts elements without requiring extra storage, modifying the input array directly.

How is merge sort implemented?

Recursively split the array, sort each half, and merge sorted results back together.

What is the space complexity of merge sort?

O(n) - This is due to the additional space needed for temporary arrays during merging.

Flashcards 2(16)

Merge sort vs. quicksort: main difference?

Merge sort is stable and divides lists into halves, while quicksort is not stable and partitions around a pivot.

True or False: Quicksort has a worst-case of O(n log n).

False. Quicksort's worst-case time complexity is O(n²) when the pivot selection is poor.

What is the space complexity of merge sort?

Merge sort has a space complexity of O(n) due to the additional arrays used for merging.

Best case scenario for quicksort?

O(n log n), when the pivot divides the array into two equal halves.

Fill in the blank: The primary method of merging in merge sort is ___ .

to combine two sorted arrays into one sorted array.

Which algorithm is generally faster for small datasets?

Quicksort is generally faster due to lower constant factors and better cache performance.

When should you use merge sort?

Use merge sort when stability is required or for large datasets that do not fit in memory.

What is a pivot in quicksort?

A pivot is an element used to partition the array into subarrays for sorting.

True or False: Merge sort can be implemented using recursion.

True. Merge sort is typically implemented using a recursive approach.

Example of a pivot selection strategy?

Random pivot, median-of-three, or first/last element can be used.

How does merge sort handle duplicates?

Merge sort handles duplicates by maintaining their relative order (stable sorting).

What is the average time complexity of merge sort?

O(n log n) consistently, regardless of the input data order.

Cause → Effect: Poor pivot choice in quicksort?

Leads to O(n²) time complexity and inefficient sorting.

Merge sort: divide and conquer strategy explained.

Divide the list into halves, sort each half, then merge them back together.

How does quicksort partition an array?

By rearranging elements so that those less than the pivot come before it and those greater come after.

Which sort is more space-efficient?

Quicksort is more space-efficient, as it requires only O(log n) space for recursion.

Questions in this Study Set(32)

1. What type of algorithm is merge sort?

A.Divide-and-conquer
B.Dynamic programming
C.Greedy
D.Backtracking

2. What is the primary characteristic that differentiates merge sort from quicksort?

A.Merge sort is stable and divides lists into halves.
B.Quicksort is stable and divides lists into halves.
C.Both algorithms are stable and divide lists into halves.
D.Merge sort is faster than quicksort in every scenario.

3. In quicksort, what is the main purpose of the pivot?

A.To determine the size of the array
B.To partition the array
C.To merge sorted arrays
D.To identify the maximum element

4. True or False: Quicksort's average-case time complexity is O(n log n).

A.True
B.False
C.Depends on the data
D.None of the above

5. True or False: Quicksort is always faster than merge sort.

A.True
B.False
C.It depends on the dataset
D.Only in small datasets

6. What is the space complexity of quicksort in the average case?

A.O(n)
B.O(log n)
C.O(1)
D.O(n log n)

7. What is the worst-case time complexity of merge sort?

A.O(n)
B.O(n log n)
C.O(log n)
D.O(n²)

8. In which scenario is merge sort particularly advantageous?

A.When sorting small datasets.
B.When sorting large datasets that cannot fit in memory.
C.When stability is not a concern.
D.When the data is already sorted.

9. Fill in the blank: The average-case time complexity of quicksort is _____ .

A.O(n)
B.O(n²)
C.O(log n)
D.O(n log n)

10. What defines a pivot in the context of quicksort?

A.The middle element of the array.
B.An element that defines the partitioning of the array.
C.The smallest element of the array.
D.The largest element of the array.

11. Which of the following statements is NOT true about merge sort?

A.It is stable.
B.It requires additional memory.
C.It is faster than quicksort in all cases.
D.It uses a divide-and-conquer approach.

12. True or False: Merge sort can be implemented both iteratively and recursively.

A.True
B.False
C.Only recursively
D.Only iteratively

13. How does quicksort handle an imbalanced partition?

A.It optimizes the pivot selection.
B.It stops sorting.
C.It resorts to merge sort.
D.It leads to O(n²) performance.

14. What is NOT a common pivot selection strategy in quicksort?

A.Median-of-three
B.Random pivot
C.First element
D.Last element

15. What is the best-case time complexity for quicksort?

A.O(n)
B.O(n log n)
C.O(n²)
D.O(log n)

16. How does merge sort treat duplicate elements during sorting?

A.It ignores them.
B.It removes them entirely.
C.It maintains their relative order.
D.It sorts them randomly.

17. What does stability mean in sorting algorithms?

A.Maintaining the original order of items
B.Being faster than other algorithms
C.Using less memory
D.Sorting in place

18. What is the average case time complexity for merge sort?

A.O(n)
B.O(n log n)
C.O(log n)
D.O(n²)

19. Which of the following statements about merge sort is true?

A.It is always in-place.
B.It can be done iteratively.
C.It is not stable.
D.It is O(n²) in the worst case.

20. Cause → Effect: Poor pivot choice in quicksort leads to what outcome?

A.Increased stability
B.Reduced time complexity
C.Decreased space usage
D.Higher time complexity

21. How is merging performed in merge sort?

A.By recursively dividing the array
B.By swapping elements
C.By combining sorted arrays
D.By randomizing the order

22. What is the main strategy of the divide-and-conquer approach in merge sort?

A.Sort the entire array at once.
B.Divide the array into two halves, sort them, and merge.
C.Only sort the left half of the array.
D.Sort elements individually before merging.

23. What is the space complexity of quicksort?

A.O(1)
B.O(n)
C.O(log n)
D.O(n log n)

24. How does quicksort rearrange elements during the partitioning process?

A.By sorting elements randomly.
B.By placing elements equal to the pivot in the middle.
C.By moving elements less than the pivot before it and greater after it.
D.By swapping adjacent elements until sorted.

25. What happens when you choose the first element as the pivot in a sorted array?

A.It leads to optimal performance.
B.It results in imbalanced partitions.
C.It enhances stability.
D.It requires fewer comparisons.

26. Which sorting algorithm is typically more space-efficient?

A.Merge sort
B.Quicksort
C.Both have the same efficiency
D.Neither is space-efficient

27. What is a base case in merge sort?

A.An array of two elements
B.An empty array
C.An array with one element
D.A fully sorted array

28. When should quicksort be avoided for large datasets?

A.When stability is required.
B.When the dataset is small.
C.When data is already sorted.
D.When poor pivot selection is likely.

29. How do you improve the performance of quicksort?

A.Use merge sort instead
B.Choose a better pivot
C.Increase the array size
D.Sort only part of the array

30. What is a practical application of merge sort?

A.Sorting small arrays.
B.Sorting linked lists.
C.Sorting arrays in place.
D.Sorting strings in alphabetical order.

31. Which of the following statements about the average-case time complexity of merge sort is true?

A.O(n log n)
B.O(n²)
C.O(n)
D.O(log n)

32. Which of the following statements is true regarding quicksort?

A.Quicksort has a worst-case time complexity of O(n²).
B.Quicksort is stable by default.
C.Quicksort always chooses the median as the pivot.
D.Quicksort requires O(n) additional space for sorting.

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.