1 of 31

CSE 332 Section 6Sorting

Original slides by James Richie Sulaeman

Revised by Rubee Zhao & Vicky Ye

2 of 31

Properties: How to choose sorting method?

  • Comparison-based / non-comparison-based: <, >, =
  • Stable: preserve the given order if =
  • In-place: no need for extra data structure to store values
  • Online: begin with an incomplete list
  • Adaptive: better running time for nearly sorted input
  • Running Time 🏃🕙

3 of 31

Comparison-based Sorting

4 of 31

Insertion Sort

Insertion sort builds a sorted subarray at the front of the original array. Takes the first element of the unsorted subarray and inserts it into the sorted subarray.

Properties:

  • In-place and stable
  • Best-case runtime: O(n)
  • Worst-case runtime: O(n²)

5 of 31

Selection Sort

Selection sort builds a sorted subarray at the front of the original array. Takes the first element of the unsorted subarray and swaps it into the correct location at the end of the sorted subarray.

Properties:

  • In-place and not usually stable
    • Can be made stable if you shift instead of swapping and break ties by selecting the left-most element
  • Best-case runtime: O(n²)
  • Worst-case runtime: O(n²)

6 of 31

Merge Sort

Merge sort recursively splits an array into two halves, sorts them, and then merges them back together to obtain a sorted array.

Properties:

  • Not in-place but stable
  • Best-case runtime: O(n log n)
  • Worst-case runtime: O(n log n)

mergeSort(input) -> sorted input:

1. sortedLeft = mergeSort(left half of input)

2. sortedRight = mergeSort(right half of input)

3. return (merged ‘sortedLeft’ and ‘sortedRight’)

7 of 31

Quick Sort

Quick sort recursively partitions an array based on a pivot element, sorts the subarrays on either side of the pivot, and merges them back together to obtain a sorted array.

Properties:

  • In-place but not stable.
  • Best-case runtime: O(n log n)
  • Worst-case runtime: O(n²)

quickSort(input) -> void:

1. pick pivot

2. partition input into ‘lessThanPivot’ and

‘greaterThanPivot’ parts

3. quickSort(lessThanPivot)

4. quickSort(greaterThanPivot)

8 of 31

Problem 0

9 of 31

Problem 0

Suppose we sort an array of numbers, but it turns out every element of the array is the same (e.g. [17, 17, 17, …, 17]).

What is the asymptotic runtime of the following sorting algorithms?

Sorting Algorithm

Asymptotic Runtime

Explanation

Insertion Sort

Selection Sort

Merge Sort

Quick Sort

10 of 31

Problem 0

Suppose we sort an array of numbers, but it turns out every element of the array is the same (e.g. [17, 17, 17, …, 17]).

What is the asymptotic runtime of the following sorting algorithms?

Sorting Algorithm

Asymptotic Runtime

Explanation

Insertion Sort

Merge Sort

Quick Sort

11 of 31

Problem 0: Solution

Sorting Algorithm

Asymptotic Runtime

Explanation

Insertion Sort

O(n)

Insertion Sort will traverse the array, but since it is already ‘sorted’, no extra computation is necessary.

Selection Sort

O(n²)

Selection Sort always has O(n²) runtime since it has to find the smallest item in the unsorted subarray n times.

Merge Sort

O(n log n)

Merge Sort always has O(n log n) runtime. You can prove this using recurrences!

Quick Sort

O(n²)

This is the worst case for Quick Sort. Since all the elements are the same, all items will end up on the same side of each partition.

12 of 31

Problem 0: Solution

Sorting Algorithm

Asymptotic Runtime

Explanation

Insertion Sort

O(n)

Insertion Sort will traverse the array, but since it is already ‘sorted’, no extra computation is necessary.

Merge Sort

O(n log n)

Merge Sort always has O(n log n) runtime. You can prove this using recurrences!

Quick Sort

O(n²)

This is the worst case for Quick Sort. Since all the elements are the same, all items will end up on the same side of each partition.

13 of 31

Problem 1

14 of 31

Problem 1

Given an array of integers as such: {11, 13, 55, 67, 79, 10, 8, 6, 4, 2}. Please answer the following questions (assume all sorts to be done in ascending order):

What is the asymptotic runtime of the following sorting algorithms?

Here, assume we choose the leftmost element as the pivot each time for QS.

Sorting Algorithm

Asymptotic Runtime

Explanation

Insertion Sort

Merge Sort

Quick Sort

15 of 31

Problem 1

Given an array of integers as such: {11, 13, 55, 67, 79, 10, 8, 6, 4, 2}.

Please answer the following questions (assume all sorts to be done in

ascending order):

What is the asymptotic runtime of the following sorting algorithms?

Here, assume we choose the leftmost element as the pivot each time for QS.

Sorting Algorithm

Asymptotic Runtime

Explanation

Insertion Sort

O(n²)

The array is not already sorted. At least half of the elements need to iterate through half of the array. So this is the worst case runtime.

Merge Sort

O(n log n)

Always has O(n log n) runtime.

Quick Sort

O(n²)

After finishing the sorting of the first pivot (11), we end up sorting the left subarray in worst-case runtime.

16 of 31

Non-Comparison Sorting

17 of 31

Bucket Sort

Distributes elements into their corresponding buckets. Buckets have an inherent ordering and are merged together in this ordering to produce the sorted array.

Properties:

  • Not in-place but stable
  • Runtime: O(n + B)
    • Need to iterate over n elements and B buckets.
    • Good when B << n or B ≈ n.
    • Bad when B >> n.

bucketSort(input) -> sorted input:

1. create array of size B

2. put each element into their corresponding bucket

3. generate sorted array by iterating through the

buckets in order

Original Input:�[51, 11, 31, 41, 32, 2, 12, 13, 52, 42, 53]

Final output:

[11, 12, 13, 2, 31, 32, 41, 42, 51, 52, 53]

Bucket Array

1

11, 12, 13

2

2

3

31, 32

4

41, 42

5

51, 52, 53

B: 5

18 of 31

Radix Sort - 1’s Digit

Radix Sort repeatedly runs bucket sort on the elements for each significant digit, from least significant to most significant.

Original input: [478, 537, 9, 721, 3, 38, 143, 67], b: 10

0

1

2

3

4

5

6

7

8

9

Bucket Sort on 1’s Digit

721

3, 143

537, 67

478, 38

9

radixSort(input) -> sorted input:

1. create array of size b

2. for each significant digit:

3. run bucket sort on the elements using their

corresponding significant digit values

0

1

2

3

4

5

6

7

8

9

Bucket Sort on 10’s Digit

721

3

143

537

67

478

537, 38

3, 9

19 of 31

Radix Sort - 10’s Digit

Radix Sort repeatedly runs bucket sort on the elements for each significant digit, from least significant to most significant.

Original input: [478, 537, 9, 721, 3, 38, 143, 67], b: 10

0

1

2

3

4

5

6

7

8

9

Bucket Sort on 10’s Digit

721

3

143

537

537, 38

3, 9

radixSort(input) -> sorted input:

1. create array of size b

2. for each significant digit:

3. run bucket sort on the elements using their

corresponding significant digit values

  • Notice how elements are now sorted with respect to their last two digits.
  • By running bucket sort from the least to the most significant digit, the order of the more significant digits take precedence over the less significant digits.

67

478

20 of 31

Radix Sort - 100’s Digit

Radix Sort repeatedly runs bucket sort on the elements for each significant digit, from least significant to most significant.

Original input: [478, 537, 9, 721, 3, 38, 143, 67], b: 10

0

1

2

3

4

5

6

7

8

9

67

478

Bucket Sort on 10’s Digit

721

3

143

537

537, 38

3, 9

0

1

2

3

4

5

6

7

8

9

Bucket Sort on 100’s Digit

3

3, 9

721

537

3, 9,

38

143

3, 9,

38, 67

478

21 of 31

Radix Sort - Final Output

Radix Sort repeatedly runs bucket sort on the elements for each significant digit, from least significant to most significant.

Original input: [478, 537, 9, 721, 3, 38, 143, 67], b: 10

0

1

2

3

4

5

6

7

8

9

Bucket Sort on 100’s Digit

3

3, 9

721

537

3, 9,

38

143

3, 9,

38, 67

478

  • Not in-place but stable
  • Runtime: O(p(n + b))
    • Bucket sort has O(n + b) runtime, and we’re running it p = logb(max(n)) times.

22 of 31

23 of 31

24 of 31

25 of 31

26 of 31

Problem 2

27 of 31

Decision Tree

28 of 31

Decision Tree: Quick Introduction

  • A decision tree is a tree-like model representing all possible decisions or comparisons in a process.
  • Nodes: points where a comparison or decision is made.
  • Branches: possible outcomes of that comparison.
  • Leaves: final outcomes after all decisions.
  • Useful for visualizing sorting, searching, or algorithm paths.

29 of 31

Decision Tree Example

a<b<c; a<c<b; b<a<c; b<c<a; c<b<a; c<a<b

a<b<c; a<c<b; c<a<b

b<a<c; b<c<a; c<b<a

Ask: is a < b?

a<b<c

a<c<b; c<a<b

b<a<c

b<c<a; c<b<a

a<c<b

c<a<b

b<c<a

c<b<a

Ask: is b<c?

Ask: is a<c?

Ask: is a<c?

Ask: is b<c?

yes

no

yes

no

yes

no

yes

no

yes

no

30 of 31

Decision Tree Example

31 of 31

Thank You!