2081 2

CSC325 ยท 2081 ยท 10 marks

What is the worst case of quick sort and how does randomize quick sort handle this problem? Sort the data -2, 4, -3, 6, 12, 10, 11, 13, 9 using quick sort.

What is the worst case of quick sort and how does randomize quick sort handle this problem? Sort the data ${-2, 4, -3, 6, 12, 10, 11, 13, 9}$ using quick sort. [10]

Design and Analysis of AlgorithmsChapter 3: Divide and Conquer AlgorithmsSorting AlgorithmsNumerical: Quick sort

Quick Sort: Worst Case, Randomized Version, and Sorting Example

Given

  • Array to sort: ${-2, 4, -3, 6, 12, 10, 11, 13, 9}$
  • Number of elements: $n = 9$
  • Marks: 10

Solution

Part A: Worst Case of Quick Sort

Quick Sort selects a pivot and partitions the array into elements smaller and larger than the pivot, then recursively sorts each part.

Worst case occurs when the pivot chosen at every step is the largest or smallest element, so the partition is maximally unbalanced: one side has $n-1$ elements, the other has $0$.

This happens (using first/last element as pivot) when the array is already sorted or reverse sorted.

Recurrence: $$T(n) = T(n-1) + T(0) + O(n) = T(n-1) + O(n)$$

Solving: $$T(n) = O(n) + O(n-1) + \dots + O(1) = O(n^2)$$

So worst case time complexity is $\mathbf{O(n^2)}$.

Part B: Randomized Quick Sort

Randomized Quick Sort picks the pivot randomly rather than always the first/last element.

RANDOMIZED-PARTITION(A, p, r):
    i = RANDOM(p, r)
    swap A[i] with A[r]
    return PARTITION(A, p, r)

RANDOMIZED-QUICKSORT(A, p, r):
    if p < r:
        q = RANDOMIZED-PARTITION(A, p, r)
        RANDOMIZED-QUICKSORT(A, p, q-1)
        RANDOMIZED-QUICKSORT(A, q+1, r)

How it helps: Since the pivot is chosen uniformly at random, no fixed input pattern (sorted/reverse sorted) can force worst-case behavior consistently. The probability of repeatedly hitting the worst split is extremely small. The expected running time becomes $O(n \log n)$ for any input, though the theoretical worst case remains $O(n^2)$ (with negligible probability).

Part C: Sorting Using Quick Sort (Lomuto, last element as pivot)

Array: $[-2, 4, -3, 6, 12, 10, 11, 13, 9]$

Step 1: pivot = 9

Elements $< 9$: $-2, 4, -3, 6$; elements $\ge 9$: $12, 10, 11, 13$

$$[-2, 4, -3, 6,; \boxed{9},; 12, 10, 11, 13]$$

Recurse on $[-2, 4, -3, 6]$ and $[12, 10, 11, 13]$.

Step 2: Left $[-2, 4, -3, 6]$, pivot = 6

All ($-2, 4, -3$) $< 6$: $$[-2, 4, -3,; \boxed{6}]$$ Recurse on $[-2, 4, -3]$.

Step 3: $[-2, 4, -3]$, pivot = -3

Both $-2, 4 \ge -3$: $$[\boxed{-3},; -2, 4]$$ Recurse on $[-2, 4]$.

Step 4: $[-2, 4]$, pivot = 4

$-2 < 4$: $$[-2,; \boxed{4}]$$ Sorted. Left portion โ†’ $[-3, -2, 4, 6]$

Step 5: Right $[12, 10, 11, 13]$, pivot = 13

$12, 10, 11 < 13$: $$[12, 10, 11,; \boxed{13}]$$ Recurse on $[12, 10, 11]$.

Step 6: $[12, 10, 11]$, pivot = 11

$10 < 11$, $12 \ge 11$: $$[10,; \boxed{11},; 12]$$ Left $[10]$ and right $[12]$ are single elements โ†’ sorted. Right portion โ†’ $[10, 11, 12, 13]$

Final Combination

$$\underbrace{[-3, -2, 4, 6]}{\text{left}} ;; \boxed{9} ;; \underbrace{[10, 11, 12, 13]}{\text{right}}$$

Final Sorted Array

$$\boxed{[-3, -2, 4, 6, 9, 10, 11, 12, 13]}$$

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.