1 of 36

Lecture 18:

Sorting, Part 3

CS 136: Spring 2024

Katie Keith

2 of 36

Record on Zoom

3 of 36

  • Welcome prospective students
  • Lab 5 due Monday or Tuesday
  • Lab 6 next week: No starter code!
  • Optional Midterm Corrections due Monday April 14 at 10pm. Submit on Gradescope.
  • TA eval form (Due April 18) : https://forms.gle/sbqCGVLAFnhUQ4i39

📣 Announcements

4 of 36

📣 Announcements

5 of 36

  • Merge Sort
    • Implement
    • Big-O
  • Quick Sort (pseudocode)
  • Wrap-Up Sorting

🎯 Today’s Learning Objectives

6 of 36

📚Readings

  • Sedgewick and Wayne. Algorithms. Section 2.2
  • Sedgewick and Wayne. Algorithms. Section 2.3

7 of 36

Sorting Algorithms

Algorithm

When?

Time Big-O?

(Auxiliary) Space Big-O?

Selection Sort

Monday Lecture

O(n2)

O(1)

Insertion Sort

Wednesday Lecture (pseudocode)

Lab 5 (implement)

O(n2)

O(1)

Merge Sort

Friday Lecture

Quick Sort

Friday Lecture (pseudocode)

Lab 5 extension (implement)

8 of 36

Merge Sort

Invented by John von Neumann in 1945.

9 of 36

Divide & Conquer

Merge sort uses the divide and conquer paradigm, a paradigm that is helpful for many different algorithms in computer science.

  • Divide: Recursively break down problems into smaller, more manageable subproblems.
  • Conquer: Combine the solutions to these subproblems to solve the original problem.

10 of 36

Merge Sort: Divide Operation

Recursively divide the unsorted array into two different arrays until each smaller array is a single element (base case).

Divide each array in half

…

11 of 36

Merge Sort: Merge operation

Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.

Do so until there is only one large sorted array remaining.

Two sorted subarrays

7

8

2

9

12 of 36

Merge Sort: Merge operation

Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.

Do so until there is only one large sorted array remaining.

Compare (smallest/

left-most elements)

7

8

2

9

13 of 36

Merge Sort: Merge operation

Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.

Do so until there is only one large sorted array remaining.

7

8

9

2

New merged & sorted array

14 of 36

Merge Sort: Merge operation

Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.

Do so until there is only one large sorted array remaining.

7

8

9

2

New merged & sorted array

Compare (smallest/

left-most elements)

15 of 36

Merge Sort: Merge operation

Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.

Do so until there is only one large sorted array remaining.

8

9

2

7

New merged & sorted array

16 of 36

Merge Sort: Merge operation

Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.

Do so until there is only one large sorted array remaining.

8

9

2

7

New merged & sorted array

Compare (smallest/

left-most elements)

17 of 36

Merge Sort: Merge operation

Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.

Do so until there is only one large sorted array remaining.

8

9

2

7

New merged & sorted array

Compare (smallest/

left-most elements)

18 of 36

Merge Sort: Merge operation

Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.

Do so until there is only one large sorted array remaining.

8

New merged & sorted array

2

7

9

19 of 36

Example

GIF credit: Wikipedia

20 of 36

MergeSort.java

đź’»

21 of 36

int i = lo;

int j = mid+1;

for (int k = lo; k <= hi; k++) {

if (i > mid) a[k] = aux[j++];

else if (j > hi) a[k] = aux[i++];

else if (aux[j]< aux[i]) a[k] = aux[j++];

else a[k] = aux[i++];

}

The chunk of code below is the important part of our merge operation for merge sort.

Use pen & paper to track how the example array a (above) changes as i, j and k change in the code.

27

38

43

3

a

27

38

43

3

aux

lo=0

mid=1

hi=3

Two sorted subarrays from previous steps

lo, mid, and hi designate the boundaries of the sorted subarrays

đź’ˇThink-pair-share

22 of 36

  • Merge Sort
    • Implement
    • Big-O
  • Quick Sort (pseudocode)
  • Wrap-Up Sorting

âś…

🎯 Today’s Learning Objectives

23 of 36

Sorting Algorithms

Algorithm

When?

Time Big-O?

(Auxiliary) Space Big-O?

Selection Sort

Monday Lecture

O(n2)

O(1)

Insertion Sort

Wednesday Lecture (pseudocode)

Lab 5 (implement)

O(n2)

O(1)

Merge Sort

Friday Lecture

Quick Sort

Friday Lecture (pseudocode)

Lab 5 extension (implement)

24 of 36

Merge-Sort Time & Space Big-O

Board work

25 of 36

  • Merge Sort
    • Implement
    • Big-O
  • Quick Sort (pseudocode)
  • Wrap-Up Sorting

âś…

âś…

âś…

🎯 Today’s Learning Objectives

26 of 36

Sorting Algorithms

Algorithm

When?

Time Big-O?

(Auxiliary) Space Big-O?

Selection Sort

Monday Lecture

O(n2)

O(1)

Insertion Sort

Wednesday Lecture (pseudocode)

Lab 5 (implement)

O(n2)

O(1)

Merge Sort

Friday Lecture

O(n log n)

O(n)

Quick Sort

Friday Lecture (pseudocode)

Lab 5 extension (implement)

27 of 36

Quicksort

Invented by Tony Hoare in 1959.

28 of 36

Quicksort overview

Overview:

  • Like Mergesort, Quicksort also uses the “divide and conquer” paradigm and is applied recursively.
  • Like Insertion Sort, Quicksort sorts an array in-place.
  • Randomized algorithm

Divide:

  • Randomly choose a pivot element.
  • Swap elements (possibly including pivot) so those with values less than the pivot come before it, while all elements with values greater than the pivot come after it; elements that are equal to the pivot can go either way

Conquer:

  • Recursively sort the two new partitions

29 of 36

Quick Sort example

Key

Filled black square = pivot element

Red outline = Positions of left pointers (i) and right pointers(j)

Black outline = Elements that are already correctly sorted

30 of 36

Quick Sort pseudocode

Shuffle the array

Recursively:

  1. Pick an element randomly in one of the unsorted sub-ranges, with index called pivot
  2. Partition the sub-range with the goal that elements smaller than array[pivot] are placed left of this element and elements larger placed right
    1. Create a left pointer (i) with at the beginning of the sub-range (i < pivot )
    2. Create a right pointer (j) with at the end of the sub-range (j > pivot)
    3. Infinite loop
      1. If the pointers indices cross (i >= j), then break
      2. Else if array[j] > array[pivot], then j--
      3. Else if array[i] < array[pivot], then i++
      4. Else swap array[j] and array[i]

31 of 36

  • Merge Sort
    • Implement
    • Big-O
  • Quick Sort (pseudocode)
  • Wrap-Up Sorting

âś…

âś…

âś…

âś…

🎯 Today’s Learning Objectives

32 of 36

Sorting Algorithms: Conclusion

Algorithm

Time

Auxiliary Space

Selection Sort

Best = Worst = O(n2)

O(1)

Insertion Sort

Best = O(n)

Worst = O(n2)

O(1)

Merge Sort

Best = Worst = O(n log n)

O(n)

Quick Sort

33 of 36

Sorting Algorithms: Conclusion

Algorithm

Time

Auxiliary Space

Selection Sort

Best = Worst = O(n2)

O(1)

Insertion Sort

Best = O(n)

Worst = O(n2)

O(1)

Merge Sort

Best = Worst = O(n log n)

O(n)

Quick Sort

Average = O(n log n)

Worst = O(n2)

O(log n)

Different partitioning strategies can change this

34 of 36

What algorithm for Python’s .sort() method?

35 of 36

Sorting algorithms as music

Start at :28

36 of 36

  • Merge Sort
    • Implement
    • Big-O
  • Quick Sort (pseudocode)
  • Wrap-Up Sorting

âś…

âś…

âś…

âś…

âś…

🎯 Today’s Learning Objectives