4.7 Review questions

This final section contains some review questions about the contents of this chapter.

4.7.1 Review questions: Mergesort

Answer TRUE or FALSE.

Mergesort (as the code is written in this chapter) is a stable sorting algorithm. Recall that a stable sorting algorithm maintains the relative order of records with equal keys.

  • What happens when two equal values are compared during merge?

You must merge 2 sorted lists of size mm and nn, respectively. The number of comparisons needed in the worst case by the merge algorithm will be:

  • Each comparison puts one record in the final sorted array.
  • You don’t compare when there is only one record.

What is the most complicated part of the Mergesort algorithm?

  • There is no partition in Mergesort.
  • The two main parts of Mergesort are splitting and merging.
  • Splitting is easy – just cut the list into two halves.
  • Merging is not hard, but it is more complicated than splitting.

In which cases are the time complexities the same for Mergesort?

  • Does Mergesort’s cost vary according to the order of the array input values?
  • No, it does not matter what order the input values have.

The order of the input records has what impact on the number of comparisons required by Mergesort (as presented in this chapter)?

  • Does Mergesort change when it make a comparison according to the order of the array input values?
  • No, it does not matter what order the input values have.

What is the worst-case time for Mergesort to sort an array of nn records?

  • Does Mergesort’s number of comparisons depend on the particular order of the input array?
  • No, it does not.

What is the running time of Mergesort when the input is an array where all record values are equal?

  • Does Mergesort’s number of comparisons depend on the particular order of the input array?
  • No, it does not.

When is Mergesort a good choice for sorting an array?

  • Does Mergesort’s number of comparisons depend on the particular order of the input array?
  • No, it does not.
  • This makes it reliable in the worst case.

In the worst case, the total number of comparisons for Mergesort is closest to: - [x] nlognn \log n - [ ] nn - [ ] n2n^2 - [ ] n2/2n^2/2

  • What is the asymptotic cost of Mergesort?
  • Only one of these answers looks like the asymptotic cost.

The array-based implementation for Mergesort uses how many arrays?

There are logn\log n passes. But the implementation is careful to reuse the auxilliary array rather than make a new one on each pass. So Mergesort requires the original array and an auxilliary array.

4.7.2 Review questions: Quicksort

Answer TRUE or FALSE.

Quicksort (as the code is written in this chapter) is a stable sorting algorithm. Recall that a stable sorting algorithm maintains the relative order of records with equal keys.

  • Think of the behaviour for partition if there are two equal key values in the array.
  • Is it possible for the two equal keys to reverse positions?

On average, how many comparisons does Quicksort require to sort 1000 records (to the nearest 1000 comparisons)?

  • What is Quicksort’s average case running time?
  • It is O(nlog2n)O(n \log_2 n)
  • This means about 10100010 \cdot 1000 comparisons are required

If it takes a given computer one second on average to run Quicksort on an array of 1000 records, how long (to the nearest thousand seconds) will it take to run Quicksort on 1,000,000 records?

(You know from this statement that the machine can do about 10,000 comparisons per second. To get the answer, you first need to compute about how many total comparisons 1,000,000 records will require.)

  • What is Quicksort’s average case running time?
  • It is O(nlog2n)O(n \log_2 n)
  • This means 10100010 \cdot 1000 instructions ran in one second for an input of 1000 records
  • What is nlog2nn \log_2 n when n=1,000,000n = 1,000,000?
  • It is about 201,000,00020 \cdot 1,000,000
  • How many seconds is 20,000,000/10,00020,000,000/10,000?

In which cases are the time complexities the same for Quicksort?

  • There are a few really bad inputs.
  • While there are a few bad inputs, they are so few as to not affect the average or best cases.

The order of the input records has what impact on the number of comparisons required by Quicksort (as presented in this chapter)?

  • Does Quicksort change when it make a comparison according to the order of the array input values?
  • Yes, the order of input can change a lot about Quicksort’s behaviour.

What is the worst-case cost for Quicksort to sort an array of n elements?

  • There are a few really bad inputs.
  • The bad cases have pivots that repeatedly reduce the partition size by one.
  • That leads to a series of partition sizes of n1n-1, n2>n-2>, and so on.
  • Since the time to process a partition is linear on its size, this in turn leads to a cost for the whole algorithm that is the sum of ii from 2 to n1n-1, that you should be very familiar with.

What is the best-case cost for Quicksort to sort an array of n elements?

  • While there are a few bad inputs, they are so few as to not affect the best case.
  • The best thing that can happen is that every pivot split its partition in half.

A disadvantage of Quicksort is:

  • How does Quicksort do in the worst case?
  • It requires O(n2)O(n^2), which is pretty bad.

(For the version of the algorithm as presented in this chapter) What is the running time of Quicksort when the input is an array where all record values are equal?

  • What does the partition step do?
  • It splits the array into a partition with all values on one side.
  • This is very bad.

Quicksort’s worst-case case cost is O(n2)O(n^2) and its average-case cost is O(nlogn)O(n \log n). This means that the fraction of input cases with cost O(n2)O(n^2) must:

  • For any size nn, it does happen that there are inputs that have cost n2n^2.
  • But if there were a constant fraction of such cases regardless of nn, then the average would also be n2n^2.
  • So the fraction must be be dropping as nn grows.

What is the worst-case cost for Quicksort’s partition step?

  • Partition moves indices inwards until the meet.
  • Each step either swaps or moves indices.
  • No record can swap more than once, and the total number of index moves can only be the length of the partition.

When selecting a pivot value, a simple thing to do is to always pick from the same place in the partition. If we use this approach, does it matter whether we always pick from the first position in the partition, the last position in the partition, or the middle position in the partition?

  • If you pick the first or last one, then sorted input will give the worst case performance.
  • If you pick the middle value, then it is still possible to get worst-case performance.
  • But to do so requires a very specific and unusual permuation that will normally occur very rarely.
  • If all permuations were equally likely, then it wouldn’t matter. But in practice, the sorted input is much more likely to occur.

When is Quicksort a good choice for sorting an array?

  • Quicksort doesn’t change its performance based on record size.
  • What are Quicksort’s average and worst case costs?
  • Quicksort’s strength is its average case cost, not its worst case cost.

4.7.3 Review questions: Comparison of sorting algorithms

Here are a few multiple choice questions that ask you to compare the sorting algorithms that we learned about in the last two chapters.

Answer TRUE or FALSE.

Selection sort is generally faster than the Bubble sort on the same input.

  • Selection sort can be viewed as an optimisation of Bubble sort.
  • On each pass, Selection sort just picks out the next record, while Bubble sort has to do a lot of swapping.

How are Selection sort and Quicksort similar?

  • Does Selection sort use divide-and-conquer? No.
  • Can Selection sort run in O(nlogn)O(n \log n) time? No.
  • How long does Quicksort need in the average case?
  • O(nlogn)O(n \log n)

(For the implementations as presented in this chapter:) Which of the following sorts is not stable?

  • Which of the mentioned algorithms will not maintain the relative order of records with equal keys?

What do we call the property of a sorting algorithm that gaurantees that records with the same key value occur in the same order in the sorted list as in the original, unsorted list?

  • “Linear” refers to an algorithm cost.
  • “External” refers to an algorithm to sort records on disk.
  • “Consistent” is not standard terminology related to sorting.
  • “Stable” sorting algorithms preserve the relative order of equal-valued keys.

A person sorting a hand of cards might reasonably use which sorting algorithm?

  • Think about each algorithm in turn, and imagine yourself trying to use it for sorting a hand of cards.
  • If you pick up the first two cards you might put them in order, then pick up the next card, and so on.
  • Or you might pick up all the cards to start, but then still sort them from left to right.
  • What sorting algorithm resembles this procedure?

Consider what happens if someone accidently calls sort on a file that is already sorted. Which of the following sorting methods will be the most efficient if the input is already in sorted order?

  • Which algorithm will make an iteration “quit early” depending on the values seen during comparisons?

Which of the following sorting methods will be best if the number of swaps done is the only measure of efficiency?

  • This sorting algorithms finds the minimum value, swaps it with the value in the first position, and repeats these steps for the remainder of the list.
  • It does no more than nn swaps.

Which of the following sorting algorithms has a worst case complexity of O(nlogn)O(n \log n)?

  • Bubble sort, Insertion sort, and Selection sort are referred to as “quadratic sorts” because of their worst-case time cost.
  • Quicksort’s worst-case behaviour comes when the pivot is chosen badly.

When Mergesort merges two sorted lists of sizes mm and nn into a sorted list of size m+nm+n, it requires how many comparisons in the worst case?

  • Merging compares two records, and selects the smaller.
  • Each record gets selected once, and each selection requires a comparison.
  • However, the last record does not need a comparison to be selected because there is no longer anything to compare it to.