CS 111 f21 — Introduction to Sorting

1 Introduction

  • What is sorting and where do we encounter it?
  • Having sorted data may make future operations much more efficient

2 Why Study Sorting?

  • Not to implement—standard libraries implement, see list.sort()
  • Excellent context for practicing analysis and design decisions
  • Very practical: you will be expected to know it

3 How would you sort a list?

3.1 Bubble Sort

  • go through the list swapping out of order elements
  • on the first pass, largest element "bubbles" up to the end
  • repeat this process, once you make a pass with no swaps, the list is sorted

3.2 Insertion Sort

  • keep a "sorted list" and an "unsorted list"
  • go through each element of the unsorted list and insert it into the appropriate position in the sorted list

3.3 Selection Sort

  • find the smallest element, swap it to the beginning
  • repeat with finding the second smallest, and so on
  • "selects" the smallest unsorted element

3.4 Bogosort

  • randomize the list until it ends up sorted

4 Insertion Sort

  • pseudocode:
for i from 1 to n - 1
    find where to insert element i into the sorted portion of the list (indexes 0 to i - 1)
    insert element i and shift other elements over

4.1 Worst-Case Analysis

Just carefully count up the steps:

when \(i=1\), 1 comparison + 1 shift
when \(i=2\), 2 comparisons + 2 shifts
when \(i=3\), 3 comparisons + 3 shifts

when \(i=n-1\), \(n-1\) comparisons + \(n-1\) shifts

sum of \(1\) through \(n-1\) is \(\frac{n(n-1)}{2}\)

\(\frac{n(n-1)}{2}\) comparisons + \(\frac{n(n-1)}{2}\) shifts

\(\frac{(n^2 - n)}{2} + \frac{(n^2 - n)}{2}\)

\(n^2 - n\)
\(O(n^2)\)

5 Selection Sort

  • pseudocode:
for i from 0 to n - 2
    find index of the smallest element, j, in range of indexes i to n - 1
    swap elements at i and j

5.1 Worst-Case Analysis

Just carefully count up the steps:

when \(i=0\), \(n-1\) comparison + \(1\) swap
when \(i=1\), \(n-2\) comparisons + \(1\) swap
when \(i=2\), \(n-3\) comparisons + \(1\) swap

when \(i=n-2\), \(1\) comparison + \(1\) swap

\(\frac{n(n-1)}{2}\) comparisons + \(n\) swaps

\(\frac{(n^2 - n)}{2} + n\)

\(\frac{n^2}{2} - \frac{n}{2} + n\)

\(\frac{n^2}{2} + \frac{n}{2}\)

\(O(n^2)\)

6 Analysis Practice

def contains(nums, x):
    for num in nums:
        if num == x:
        return True
    return False

This will be \(O(n)\) since it loops over each list element once (i.e., goes around the loop \(n\) times for a list with \(n\) elements)

def max(a, b):
    """assume a and b are numbers"""
    if a >= b:
        return a
    return b

This will be \(O(1)\) because the number of operations does not depend on the input

def count_digits(x):
    digits = 0
    while x >= 1:
        digits += 1
    x = x / 10
    return digits

This will be \(O(\log_{10}n)\) because \(\log_{10}n\) is how many times the while loop will iterate when \(x=n\)