THINK FIRST·CODE LATER

Answered 0/32 Correct 0
Sign in to save progress across devices
Q1

In their usual array implementations, which of the simple sorts from CPS 1231 is not stable?

Q2

What does this code print?

int[] a = {5, 2, 9, 1, 7, 3};
for (int i = 1; i <= 3; i++) {
    int key = a[i];
    int j = i - 1;
    while (j >= 0 && a[j] > key) {
        a[j + 1] = a[j];
        j--;
    }
    a[j + 1] = key;
}
System.out.println(Arrays.toString(a));
Q3

Insertion sort is run on an array of n elements that is already sorted. How many element comparisons (a[j] > key) does it make?

Q4

A sensor log of n readings is "almost sorted": every reading is at most 3 positions away from where it belongs. What is the running time of insertion sort on it?

Q5

What is the extra memory used by the array merge sort in this chapter, apart from the recursion stack?

Q6

The merge method of this chapter merges the sorted halves [1, 4, 6] and [2, 3, 5]. How many times is the comparison a[i] <= a[j] evaluated?

Q7

A student writes the merge test as if (a[i] < a[j]) instead of if (a[i] <= a[j]). What changes?

Q8

What is the running time of the top-down merge sort of this chapter on an array that is already sorted?

Q9

The Lomuto partition(a, 0, 6) from this chapter is called on a = {7, 2, 9, 4, 3, 8, 5}. What is the array afterwards, and what does the method return?

Q10

The Lomuto partition(a, 0, 4) is called on a = {4, 8, 1, 6, 3}. What is the result?

Q11

Quick sort with the Lomuto partition (pivot = last element) sorts the already sorted array {0, 1, 2, 3, 4, 5, 6, 7}. How many element comparisons are made in total?

Q12

Which pivot rule stops quick sort from going quadratic on already sorted or reverse-sorted arrays?

Q13

The quickSort of this chapter recurses on both sides of the pivot. In the worst case, how deep can the recursion get for n elements?

Q14

Why is quick sort with the Lomuto partition not a stable sort?

Q15

Which statement about heap sort is correct?

Q16

Counting sort (values 0..4) is applied to {3, 1, 4, 1, 0, 3}. What does the count array contain after the prefix-sum step, before any element is placed?

Q17

For which task is counting sort a poor choice?

Q18

LSD radix sort (base 10, stable counting pass per digit) is applied to {170, 45, 75, 90, 802, 24, 2, 66}. What is the array after the first pass (units digit)?

Q19

Why must each digit pass of LSD radix sort be stable?

Q20

Bucket sort puts each value x from [0, 1) into bucket (int) (x * n) (n buckets), insertion-sorts each bucket, and concatenates. When is its expected time O(n)?

Q21

In the decision-tree argument for the lower bound on comparison sorting, why must the tree for inputs of size n have at least n! leaves?

Q22

What is the smallest number of comparisons that any comparison-based algorithm needs, in the worst case, to sort 4 distinct elements?

Q23

What does this code print?

String[] w = {"pear", "fig", "kiwi", "plum", "apple", "date"};
Arrays.sort(w, (x, y) -> x.length() - y.length());
System.out.println(Arrays.toString(w));
Q24

What happens when you compile and run this code?

int[] a = {3, 1, 2};
Arrays.sort(a, (x, y) -> y - x);
System.out.println(Arrays.toString(a));
Q25

Which statement about the Java library's sorts is correct?

Q26

Which algorithm is the best choice for each task? (1) 5 million exam marks, integers 0–100. (2) 1 million order objects already sorted by customer, to be re-sorted by date while keeping orders with the same date in customer order.

Q27 Short answer

Explain why quick sort with a last-element pivot takes quadratic time on an already sorted array, and give two ways to avoid it.

Q28 Short answer

Sketch the decision-tree argument showing that every comparison-based sorting algorithm needs Ω(n log n) comparisons in the worst case.

Q29 Short answer

LSD radix sort sorts n 32-bit integers in O(n) time (4 passes, one per byte). Does this contradict the Ω(n log n) lower bound? Explain.

Q30 Short answer

Arrays.sort(int[]) and Arrays.sort(Object[]) use different algorithms. Name them and explain the reason for the difference.

Q31 Programming

Write public static long countInversions(int[] a) that returns the number of pairs i < j with a[i] > a[j] in O(n log n) time, without modifying a. For {2, 4, 1, 3, 5} it returns 3. Hint: adapt merge sort — when an element of the right half is copied before the remaining elements of the left half, how many inversions does that reveal?

Q32 Programming

Write public static int select(int[] a, int k) that returns the k-th smallest element (k = 0 is the minimum) using the Lomuto partition, without sorting the whole array (quickselect). It may rearrange a. For {7, 2, 9, 4, 3} and k = 2 it returns 4.