Chapter 3: Sorting Algorithms

Chapter 3: Sorting Algorithms · Study Questions
Stability and in-place sortingBubble sortInsertion sortSelection sortShell sortMerge sortQuicksortHeapsortCounting and radix sortComparison
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Sorting Criteria

Question 1: An Unstable Algorithm

Records are sorted by the number only: (3, A), (3, C), (1, B). Which of the following algorithms (with the code from the slides) can change the relative order of A and C?

C: Selection sort swaps the smallest, (1, B), with the first, (3, A), in the first pass: (1, B), (3, C), (3, A). A long-distance swap breaks the order of equal keys. Bubble sort (swaps only if >), insertion sort (shifts only elements that are >) and merge sort (takes from the left on ties) are stable.

Step-by-Step Trace · Bubble Sort

Question 2: Flagged Bubble Sort: Passes

Flagged bubble sort is applied to [5, 2, 4, 6, 1, 3]. What is the array at the end of each pass, and in which pass does the algorithm stop?

524613012345

n = 6. Each pass compares adjacent pairs from left to right; at the end of the pass the largest of the remaining elements settles at the end.

after pass i = 0245136012345

Pass i = 0: 5 comparisons, 4 swaps. 6 settled in its final place (index 5); the flag is true.

after pass i = 1241356012345

Pass i = 1: 4 comparisons, 2 swaps. 5 settled in its final place (index 4); the flag is true.

after pass i = 2213456012345

Pass i = 2: 3 comparisons, 2 swaps. 4 settled in its final place (index 3); the flag is true.

after pass i = 3123456012345

Pass i = 3: 2 comparisons, 1 swap. 3 settled in its final place (index 2); the flag is true.

after pass i = 4123456012345

Pass i = 4: 1 comparison, no swaps → the flag is false → break. Total 5 + 4 + 3 + 2 + 1 = 15 comparisons, 9 swaps.

boolean swapped;
for(int i = 0; i < n - 1; i++) {
  swapped = false;
  for(int j = 0; j < n - 1 - i; j++) {
    if(arr[j] > arr[j+1]) {
      int temp = arr[j];
      arr[j] = arr[j+1];
      arr[j+1] = temp;
      swapped = true;
    }
  }
  if(swapped == false) break;
}

Multiple Choice · Bubble Sort

Question 3: The Flag on Sorted Input

If the flagged bubble sort is applied to an already sorted (ascending) array of 1000 elements, how many comparisons are made?

C: In the first pass 999 adjacent pairs are compared and no swap happens; the flag stays false and the loop exits with break: n − 1 = 999 → Θ(n). The version without the flag would make 499,500 = n(n − 1)/2 comparisons.

Output Question · Insertion Sort

Question 4: Number of Comparisons and Shifts

While insertion sort sorts [5, 2, 4, 6, 1, 3], how many times is the comparison arr[j] > key made, and how many shifts are made?

for(int i = 1; i < n; i++) {
  int key = arr[i];
  int j = i - 1;
  while(j >= 0 && arr[j] > key) {
    arr[j+1] = arr[j];
    j = j - 1;
  }
  arr[j+1] = key;
}
Show answer
ikeycomp.shiftsafter
12112 5 4 6 1 3
24212 4 5 6 1 3
36102 4 5 6 1 3
41441 2 4 5 6 3
53431 2 3 4 5 6

Total 12 comparisons, 9 shifts. Shifts = number of inversions (9). When j drops to −1 at i = 1 and i = 4, j >= 0 is false and no extra comparison is made.

Multiple Choice · Insertion Sort

Question 5: Insertion with Binary Search

If, in insertion sort, the position where the key goes is found in the sorted part with binary search, what is the worst-case time complexity?

C: The comparisons drop to O(n log n), but to make room for the key the larger elements must still be shifted one by one; on reverse-sorted input that is 1 + 2 + … + (n − 1) shifts → Θ(n²) in total.

Step-by-Step Trace · Insertion Sort

Question 6: Printing After Each Pass

What does insertion sort, printing after every outer pass, print for A = {5, 7, 3, 4, 2, 8}?

573428012345placed

i = 1: key = 7, A[0] = 5 ≤ 7 → the while loop never runs; 7 stays in place. The array is printed unchanged.

557428012345j+1

key = 3. The 2 larger elements were each shifted one place right; when j = -1 the condition becomes false. The vacated cell is j + 1 = 0.

357428012345placed

A[0] = 3; A[0..2] is sorted. The line is printed.

355728012345j+1

key = 4. The 2 larger elements were each shifted one place right; when j = 0 the condition becomes false. The vacated cell is j + 1 = 1.

345728012345placed

A[1] = 4; A[0..3] is sorted. The line is printed.

334578012345j+1

key = 2. The 4 larger elements were each shifted one place right; when j = -1 the condition becomes false. The vacated cell is j + 1 = 0.

234578012345placed

A[0] = 2; A[0..4] is sorted. The line is printed.

234578012345placed

i = 5: key = 8, the 7 to its left is ≤ 8 → no shift, 8 stays in place. The array is printed unchanged. The loop ends: 5 lines were printed, and the last line is the sorted array.

for (int i = 1; i < A.length; ++i) {
  int key = A[i];
  int j = i - 1;
  while (j >= 0 && A[j] > key) {
    A[j + 1] = A[j];
    j = j - 1;
  }
  A[j + 1] = key;
  System.out.println(Arrays.toString(A));
}

Multiple Choice · Insertion Sort

Question 7: Case and Bound

Which statement is true about the running time of insertion sort?

D: On sorted input there is one comparison per key: n − 1 → Θ(n); on reverse-sorted input n(n − 1)/2 → Θ(n²). Since the lower and upper bounds are different functions, a single Θ cannot be written for all inputs; the inference in C is wrong. Even without shifts, n − 1 comparisons are made, so B is wrong.

Output Question · Selection Sort

Question 8: Selection Sort: Console Output

For the array [7, 3, 9, 4, 2], what does the following code print? How many actual swaps are made?

for (int i = 0; i < arr.length - 1; i++) {
  int minIdx = i;
  for (int j = i + 1; j < arr.length; j++) {
    if (arr[j] < arr[minIdx]) {
      minIdx = j;
    }
  }
  if (minIdx != i) {
    swap(arr, i, minIdx);
  }
  System.out.println(Arrays.toString(arr));
}
Show answer
[2, 3, 9, 4, 7]
[2, 3, 9, 4, 7]
[2, 3, 4, 9, 7]
[2, 3, 4, 7, 9]
  • i = 0: the smallest, 2 (index 4) ↔ 7.
  • i = 1: the smallest is already 3 → minIdx == i, no swap; the line is printed unchanged.
  • i = 2: 4 ↔ 9. i = 3: 7 ↔ 9.

3 swaps; the number of comparisons is 4 + 3 + 2 + 1 = 10 in every case → Θ(n²).

Step-by-Step Trace · Shell Sort

Question 9: Tracing Shell Sort

shellSort is applied to [9, 8, 3, 7, 5, 6, 4, 1]. What is the array after each gap value?

start9837564101234567

n = 8; the gap sequence is n/2 = 4, 2, 1. Elements of the same color (indices a gap apart) are insertion-sorted together.

gap = 4: groups9837564101234567

gap = 4: groups [9, 5], [8, 6], [3, 4], [7, 1].

after gap = 45631984701234567

Each group was sorted internally (3 shifts) → [5, 6, 3, 1, 9, 8, 4, 7].

gap = 2: groups5631984701234567

gap = 2: groups [5, 3, 9, 4], [6, 1, 8, 7].

after gap = 23146579801234567

Each group was sorted internally (5 shifts) → [3, 1, 4, 6, 5, 7, 9, 8].

after gap = 11345678901234567

gap = 1 is ordinary insertion sort; since the array is almost sorted, only 3 shifts were needed. Result [1, 3, 4, 5, 6, 7, 8, 9].

void shellSort(int[] arr) {
  int n = arr.length;
  for (int gap = n / 2; gap > 0;
                        gap /= 2)
    for (int i = gap; i < n; i++) {
      int key = arr[i], j = i;
      while (j >= gap
             && arr[j - gap] > key) {
        arr[j] = arr[j - gap];
        j -= gap;
      }
      arr[j] = key;
    }
}

Multiple Choice · Shell Sort

Question 10: Properties of Shell Sort

Which statement is true for Shell sort (with gaps n/2, n/4, …, 1)?

D: Earlier passes move small elements from far away to the front in one move; that is why the gap = 1 pass does little work. Shell sort is in place (O(1) extra memory) and unstable: equal elements in different groups can jump over each other.

Step-by-Step Trace · Merge Sort

Question 11: Merging Two Sorted Arrays

The merge step is applied to A = [4, 6, 15, 18] and B = [2, 9, 10, 21]. In what order is C filled, and how many comparisons are made?

A461518iB291021jC01234567k

Two sorted arrays: A = [4, 6, 15, 18], B = [2, 9, 10, 21]. Only the elements at the front of the two arrays (yellow) are compared; C is empty.

A461518iB291021jC201234567k

A[0] = 4 > B[0] = 2 → C[0] = 2; j and k advance.

A461518iB291021jC2401234567k

A[0] = 4 ≤ B[1] = 9 → C[1] = 4; i and k advance.

A461518iB291021jC24601234567k

A[1] = 6 ≤ B[1] = 9 → C[2] = 6; i and k advance.

A461518iB291021jC246901234567k

A[2] = 15 > B[1] = 9 → C[3] = 9; j and k advance.

A461518iB291021jC24691001234567k

A[2] = 15 > B[2] = 10 → C[4] = 10; j and k advance.

A461518iB291021jC2469101501234567k

A[2] = 15 ≤ B[3] = 21 → C[5] = 15; i and k advance.

A461518B291021jC246910151801234567k

A[3] = 18 ≤ B[3] = 21 → C[6] = 18; i and k advance. A is finished; the first while loop ends.

A461518B291021C24691015182101234567

The remaining 21 in B is copied without a comparison. Result [2, 4, 6, 9, 10, 15, 18, 21]: 8 writes, 7 comparisons → Θ(n).

int i = 0, j = 0, k = 0;
while (i < A.length && j < B.length) {
  if (A[i] <= B[j])
    C[k++] = A[i++];
  else
    C[k++] = B[j++];
}
while (i < A.length)
  C[k++] = A[i++];
while (j < B.length)
  C[k++] = B[j++];

Output Question · Merge Sort

Question 12: Calls to merge

mergeSort(arr, 0, 6) is called on the array [38, 27, 43, 3, 9, 82, 10]. How many times is merge called? What is the result of the first four merges (the merged range)?

void mergeSort(int[] arr,
               int left, int right) {
  if (left < right) {
    int mid = left + (right - left) / 2;
    mergeSort(arr, left, mid);
    mergeSort(arr, mid + 1, right);
    merge(arr, left, mid, right);
  }
}
Show answer

merge is called 6 times (n − 1). In order:

  1. [0..1] → [27, 38]
  2. [2..3] → [3, 43]
  3. [0..3] → [3, 27, 38, 43]
  4. [4..5] → [9, 82]
  5. [4..6] → [9, 10, 82]
  6. [0..6] → [3, 9, 10, 27, 38, 43, 82]

The right half is not started until the left half is completely sorted (depth-first). Because mid = left + (right − left)/2, the left half gets one extra element: [0..3] and [4..6].

Multiple Choice · Merge Sort

Question 13: Most Comparisons for 8 Elements

Merge sort sorts an array of 8 elements with at most how many element comparisons in the worst case?

B: Merging m elements takes at most m − 1 comparisons. By level: 4 merges × 1 + 2 × 3 + 1 × 7 = 17. 24 = n log₂ n is an upper estimate; 28 = n(n − 1)/2 is the count for the simple algorithms; 7 is only the last merge.

Multiple Choice · Merge Sort

Question 14: Which One on a Tie?

In the merge method, what happens if the condition L[i] <= R[j] is changed to L[i] < R[j]?

B: On a tie, the element from the right half (R) is now taken first; the relative order of two records with equal keys is broken. The result is still sorted; only stability is lost. This difference matters if the records were previously sorted by another field.

Classic Question · Merge Sort

Question 15: Counting Inversions

Propose a method that counts the inversions in an array (pairs with i < j and arr[i] > arr[j]) in O(n log n) time. Find the result for [3, 1, 2, 5, 4].

Show answer

Merge sort is modified as follows: in merge, when an element is taken from the right (from R), every element remaining in L is larger than it and forms an inversion with it: the counter grows by L.length − i. The inversions inside the left and right halves are counted recursively.

[3, 1, 2, 5, 4]: inversions (3,1), (3,2), (5,4) → 3. This number is the number of shifts of insertion sort and the number of swaps of bubble sort.

The total work is the same as for merge sort: T(n) = 2T(n/2) + Θ(n) = Θ(n log n). Brute force (all pairs) takes Θ(n²).

Step-by-Step Trace · Quicksort

Question 16: Lomuto Partitioning

partition(arr, 0, 7) is run on [7, 2, 1, 6, 8, 5, 3, 4]. How do i and the array change at each j, and what does the method return?

7216853401234567j

pivot = arr[7] = 4 (dark); i = −1: the ≤ pivot zone (blue) is empty. Red: > pivot, white: not examined yet.

7216853401234567j

j = 0: 7 > 4 → only j advances; the red zone grows.

2716853401234567ij

j = 1: 2 ≤ 4 → i = 0; swap(arr, 0, 1) → [2, 7, 1, 6, 8, 5, 3, 4].

2176853401234567ij

j = 2: 1 ≤ 4 → i = 1; swap(arr, 1, 2) → [2, 1, 7, 6, 8, 5, 3, 4].

2176853401234567ij

j = 3: 6 > 4 → only j advances; the red zone grows.

2176853401234567ij

j = 4: 8 > 4 → only j advances; the red zone grows.

2176853401234567ij

j = 5: 5 > 4 → only j advances; the red zone grows.

2136857401234567i

j = 6: 3 ≤ 4 → i = 2; swap(arr, 2, 6) → [2, 1, 3, 6, 8, 5, 7, 4].

2134857601234567pivot

The loop is over: swap(arr, i + 1 = 3, 7) → [2, 1, 3, 4, 8, 5, 7, 6]. return 3: the pivot is in its final place; its left side is ≤ 4, its right side is > 4. 7 comparisons → Θ(n).

int partition(int[] arr, int lo, int hi) {
  int pivot = arr[hi];
  int i = lo - 1;
  for (int j = lo; j < hi; j++) {
    if (arr[j] <= pivot) {
      i++;
      swap(arr, i, j);
    }
  }
  swap(arr, i + 1, hi);
  return i + 1;
}

Multiple Choice · Quicksort

Question 17: Sorted Input and a Last-Element Pivot

Quicksort with the last element as the pivot is called on an already sorted array of 1000 elements. How many comparisons does it make, and what is the recursion depth?

B: The pivot is the largest element every time; partitioning separates only the pivot: 999 + 998 + … + 1 = 499,500 comparisons and n levels of recursion (T(n) = T(n − 1) + Θ(n) → Θ(n²)). A random pivot or the median of three avoids this case. 9,966 ≈ n log₂ n is the count for balanced splits.

Step-by-Step Trace · Heapsort

Question 18: Building a Max-Heap

The first phase of heapSort (building the heap) is applied to [2, 7, 4, 1, 8, 6, 5]. Which swaps are made, and what is the resulting max-heap?

2741865

The array is viewed as a complete binary tree: the children of arr[i] are 2i + 1 and 2i + 2. The leaves (i ≥ 3) are already one-element heaps; sifting down starts at i = n/2 − 1 = 2 and moves toward the root.

2761845

heapify(i = 2): 4 is smaller than its largest child → 4 ↔ 6 (index 2 ↔ 5). Array [2, 7, 6, 1, 8, 4, 5].

2861745

heapify(i = 1): 7 is smaller than its largest child → 7 ↔ 8 (index 1 ↔ 4). Array [2, 8, 6, 1, 7, 4, 5].

8261745

heapify(i = 0): 2 is smaller than its largest child → 2 ↔ 8 (index 0 ↔ 1). Array [8, 2, 6, 1, 7, 4, 5].

8761245

The sift-down continues: 2 is smaller than its largest child → 2 ↔ 7 (index 1 ↔ 4). Array [8, 7, 6, 1, 2, 4, 5]. The max-heap is ready: the root 8 is the largest element.

7561248

First iteration of phase 2: root 8 ↔ arr[6] = 5 → 8 moves into the sorted zone (dashed); heap size 6. heapify(0): 5 ↔ 7 → [7, 5, 6, 1, 2, 4, 8].

void heapSort(int[] arr) {
  int n = arr.length;
  for (int i = n / 2 - 1; i >= 0; i--)
    heapify(arr, n, i);
  // phase 2: move the root to the end, sift down ...
}

Multiple Choice · Heapsort

Question 19: Properties of Heapsort

Which of the following is true for heapsort?

D: The root is moved to the end n − 1 times and sifted down in O(log n); no extra array is needed. Bottom-up construction is O(n) (so A is backwards), the root–last swap breaks stability (B is false), and heapsort is not adaptive: even sorted input takes Θ(n log n).

Multiple Choice · Linear-Time Sorting

Question 20: The Decision-Tree Bound

How many comparisons must every algorithm that sorts 3 distinct elements using only comparisons make, at least, in the worst case?

B: The decision tree must have 3! = 6 leaves; a binary tree of height h has at most 2ʰ leaves: 2ʰ ≥ 6 ⇒ h ≥ ⌈log₂ 6⌉ = 3. Two comparisons can distinguish only 4 outcomes. In general, log₂(n!) = Ω(n log n).

Step-by-Step Trace · Linear-Time Sorting

Question 21: Tracing Counting Sort

countingSort is run on [3, 1, 4, 1, 3, 0]. How are the count array and the output array filled?

arr314130012345count1202101234output012345

The counting loop is done: count[x] = the number of occurrences of key x → [1, 2, 0, 2, 1]. Since max = 4, the count array has 5 cells.

arr314130012345count1335601234output012345

Prefix sums: count[x] = the number of elements with key ≤ x → [1, 3, 3, 5, 6]. count[x] − 1 is the last position of x in the output.

arr314130012345count0335601234output0012345

i = 5: arr[5] = 0 → output[count[0] − 1] = output[0] = 0; count[0] = 0.

arr314130012345count0334601234output03012345

i = 4: arr[4] = 3 → output[count[3] − 1] = output[4] = 3; count[3] = 4.

arr314130012345count0234601234output013012345

i = 3: arr[3] = 1 → output[count[1] − 1] = output[2] = 1; count[1] = 2.

arr314130012345count0234501234output0134012345

i = 2: arr[2] = 4 → output[count[4] − 1] = output[5] = 4; count[4] = 5.

arr314130012345count0134501234output01134012345

i = 1: arr[1] = 1 → output[count[1] − 1] = output[1] = 1; count[1] = 1.

arr314130012345count0133501234output011334012345

i = 0: arr[0] = 3 → output[count[3] − 1] = output[3] = 3; count[3] = 3. Output: [0, 1, 1, 3, 3, 4]. No comparisons: Θ(n + k).

int[] countingSort(int[] arr) { // elements ≥ 0
  if (arr.length == 0) return arr;
  int max = Arrays.stream(arr).max().getAsInt();
  int[] count = new int[max + 1];
  for (int x : arr) count[x]++;
  for (int i = 1; i <= max; i++)
    count[i] += count[i - 1];
  int[] output = new int[arr.length];
  for (int i = arr.length - 1; i >= 0; i--) {
    output[count[arr[i]] - 1] = arr[i];
    count[arr[i]]--;
  }
  return output;
}

Output Question · Linear-Time Sorting

Question 22: Radix Sort Passes

[329, 457, 657, 839, 436, 720, 355] is sorted with LSD radix sort (a stable counting sort in each pass). What is the array after each pass?

Show answer
PassKeyArray
1ones[720, 355, 436, 457, 657, 329, 839]
2tens[720, 329, 436, 839, 355, 457, 657]
3hundreds[329, 355, 436, 457, 657, 720, 839]

In pass 2, 720 and 329, which both have tens digit 2, keep the order they had after pass 1 (stability). An unstable intermediate sort would destroy the order from earlier digits. d = 3, b = 10: Θ(d(n + b)).

Multiple Choice · Comparison

Question 23: Which Algorithm?

Which algorithm is best suited for sorting a large file on disk that does not fit in memory, or for sorting a linked list?

A: Merge sort needs no random access; it reads and writes elements sequentially. A file is sorted in pieces and merged, and a linked list can be merged without an extra array. Heapsort and quicksort rely on random access by index.

The End

Chapter 3: Sorting Algorithms: Study Questions
Lecture slides: Chapter 3: Sorting Algorithms · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.