Lecture 5/15: Sorting


May 15, 2020

πŸ“‚Associated files

Sorting

CS 106B: Programming Abstractions

Spring 2020, Stanford University Computer Science Department

Lecturers: Chris Gregg and Julie Zelenski

A lego man sorting red and blue lego pieces into boxes.


Slide 2

Announcements

Introduction to Sorting


Slide 3

Sorting!


Slide 4

Sorts


Slide 5

Insertion Sort


Slide 6

Insertion Sort Algorithm


Slide 7

Insertion sort Code

// Rearranges the elements of v into sorted order.
void insertionSort(Vector<int>& v) {
    for (int i = 1; i < v.size(); i++) {
        int temp = v[i];
        // slide elements right to make room for v[i]
        int j = i;
        while (j >= 1 && v[j - 1] > temp) {
            v[j] = v[j - 1];
            j--; 
        }
        v[j] = temp;
    }
}

Slide 8

Selection Sort


Slide 9

Selection Sort Example


Slide 10

Selection sort Code

// Rearranges elements of v into sorted order
// using selection sort algorithm
void selectionSort(Vector<int>& v) {
    for (int i = 0; i < v.size() - 1; i++) {
        // find index of smallest remaining value
        int min = i;
        for (int j = i + 1; j < v.size(); j++) {
            if (v[j] < v[min]) {
                min = j;
            }
        }
        // swap smallest value to proper place, v[i]
        if (i != min) {
            int temp = v[i];
            v[i] = v[min];
            v[min] = temp;
        }
    }
}

Slide 11

Merge Sort


Slide 12

Merging two sorted lists


Slide 13

Merge Sort full algorithm


Slide 14

Merge Sort, full example


Slide 15

Merge Sort: Time Complexity


Slide 16

Merge Sort: Space Complexity


Slide 17

Is our merge sort stable?


Slide 18

Quicksort


Slide 19

Quicksort Algorithm


Slide 20

Quicksort: different methods, same underlying algorithm


Slide 21

Quicksort: Naive implementation


Slide 22

Quicksort algorithm: Naive code

Vector<int> naiveQuickSort(Vector<int> v) { // not passed by reference!
    // base case: list of 0 or 1
    if (v.size() < 2) {
        return v;
    }
    int pivot = v[0];    // choose pivot to be left-most element
    
    // create two new vectors to partition into
    Vector<int> left, right;
    
    // put all elements <= pivot into left, and all elements > pivot into right
    for (int i=1; i<v.size(); i++) {
        if (v[i] <= pivot) {
            left.add(v[i]);
        }
        else {
            right.add(v[i]);
        }
    }
    left = naiveQuickSortHelper(left); // recursively handle the left
    right = naiveQuickSortHelper(right); // recursively handle the right
    
    left.add(pivot); // put the pivot at the end of the left
    
    return left + right; // return the combination of left and right
}

Slide 23

Quicksort algorithm: in-place


Slide 24

Quicksort in-place code

/*
 * Rearranges the elements of v into sorted order using
 * a recursive quick sort algorithm.
 */
void quicksort(Vector<int> &vec) {
    quicksort(vec, 0, vec.size() - 1);
}

// helper function to pass in start and finish
void quicksort(Vector<int> &vec, int start, int finish) {
    if (start >= finish) return;
    int boundary = partition(vec, start, finish);
    quicksort(vec, start, boundary - 1);
    quicksort(vec, boundary + 1, finish);
}

Slide 25

Quicksort Complexity


Slide 26

Recap

Sorting Big-O Cheat Sheet
Sort Worst Case Best Case Average Case
Insertion O(n^2) O(n) O(n^2)
Selection O(n^2) O(n^2) O(n^2)
Merge O(n log n) O(n log n) O(n log n)
Quicksort O(n^2) O(n log n) O(n log n)