CPSC 221

Insertion Sort

Today

Announcements

  • PA1 is out, due Monday Oct 5. It is linked lists all the way down
  • Examlet 1 is Thursday through Sunday — book your seat on PrairieTest
  • PEX2 releases tonight
  • HW1 closed Monday


Where we’re going

  • Finishing selection sort
  • A mystery function
  • Insertion sort

Where we left off

void selectionSort (vector<int>& C) {
    for (int i = 0; i < C.size(); i++) {
        int m = idxOfMin(C, i);
        swap(C, m, i);
    }
}

Thm.   After selectionSort(C) returns, C is a permutation of \(C_0\) and \(C[0] \le C[1] \le \cdots \le C[n-1]\).


Loop invariant.   \(P(i)\), at the start of iteration \(i\):
  A.  \(C\) is a permutation of \(C_0\)
  B.  \(C[0] \le C[1] \le \cdots \le C[i-1]\)
  C.  \(\max_{0 \le k < i} C[k] \;\le\; \min_{i \le j < n} C[j]\)

Initialization

P(i) at the start of iteration \(i\):

A.  \(C\) is a permutation of \(C_0\)

B.  \(C[0] \le C[1] \le \cdots \le C[i-1]\)

C.  \(\max_{0 \le k < i} C[k] \;\le\; \min_{i \le j < n} C[j]\)

void selectionSort (vector<int>& C) {
    for (int i = 0; i < C.size(); i++) {
        int m = idxOfMin(C, i);
        swap(C, m, i);
    }
}

Check that the loop invariant holds at the start of the loop:

(\(i =\) ___)

Restoration

P(i) at the start of iteration \(i\):

A.  \(C\) is a permutation of \(C_0\)

B.  \(C[0] \le C[1] \le \cdots \le C[i-1]\)

C.  \(\max_{0 \le k < i} C[k] \;\le\; \min_{i \le j < n} C[j]\)

void selectionSort (vector<int>& C) {
    for (int i = 0; i < C.size(); i++) {
        int m = idxOfMin(C, i);
        swap(C, m, i);
    }
}

Assume the invariant at the start of iteration \(i\).

Run the body:

  Line 3:

  Line 4:

Check the invariants:

  A.

  B.

  C.

Termination

P(i) at the start of iteration \(i\):

A.  \(C\) is a permutation of \(C_0\)

B.  \(C[0] \le C[1] \le \cdots \le C[i-1]\)

C.  \(\max_{0 \le k < i} C[k] \;\le\; \min_{i \le j < n} C[j]\)

void selectionSort (vector<int>& C) {
    for (int i = 0; i < C.size(); i++) {
        int m = idxOfMin(C, i);
        swap(C, m, i);
    }
}

Does the loop stop?

What is the state when it does?

A mystery function

void ________ (vector<int>& C, int loc) {
// assumes C[0..loc-1] is sorted, loc valid
    int j = loc;
    while (j > 0 && C[j-1] > C[j]) {
        swap(C, j-1, j);
        j--;
    }
}
0 1 2 3 4 5 6 7
8 14 26 32 19 35 2 10

What does it do?    How long does it take?    Is it correct?

Is slide correct?

void slide (vector<int>& C, int loc) {
// assumes C[0..loc-1] is sorted, loc valid
    int j = loc;
    while (j > 0 && C[j-1] > C[j]) {
        swap(C, j-1, j);
        j--;
    }
}

Thm.   If C[0..loc-1] is sorted, then afterwards C is a permutation of \(C_0\) and C[0..loc] is sorted.

  • Variable: \(j\), from ___ down to ___
  • Loop invariant \(Q(j)\), at the top of the test:
      A.  C is a _______ of \(C_0\)
      B.  C[0..___] is sorted, and C[___..loc] is sorted
      C.  \(\max_{0 \le k < j} C[k] \;\le\; \min_{j < k \le \mathit{loc}} C[k]\)

slide, continued

Q(j) at the top of the test:

A.  \(C\) is a permutation of \(C_0\)

B.  \(C[0..j-1]\) and \(C[j..\mathit{loc}]\) are each sorted

C.  \(\max_{0 \le k < j} C[k] \;\le\; \min_{j < k \le \mathit{loc}} C[k]\)

void slide (vector<int>& C, int loc) {
// assumes C[0..loc-1] is sorted, loc valid
    int j = loc;
    while (j > 0 && C[j-1] > C[j]) {
        swap(C, j-1, j);
        j--;
    }
}

Initialization:   \(j =\) ___


Restoration:

slide, termination

Q(j) at the top of the test:

A.  \(C\) is a permutation of \(C_0\)

B.  \(C[0..j-1]\) and \(C[j..\mathit{loc}]\) are each sorted

C.  \(\max_{0 \le k < j} C[k] \;\le\; \min_{j < k \le \mathit{loc}} C[k]\)

void slide (vector<int>& C, int loc) {
// assumes C[0..loc-1] is sorted, loc valid
    int j = loc;
    while (j > 0 && C[j-1] > C[j]) {
        swap(C, j-1, j);
        j--;
    }
}

The loop exits when __________   or   __________


Case 1:


Case 2:

Insertion sort

void insertionSort (vector<int>& C) {
    for (int i = 1; i < C.size(); i++)
        slide(C, i);
}
0 1 2 3 4 5 6 7
32 14 26 8 19 35 2 10

Worst case running time:

0 1 2 3 4 5 6 7

Best case?

0 1 2 3 4 5 6 7

Insertion sort — correctness

void insertionSort (vector<int>& C) {
    for (int i = 1; i < C.size(); i++)
        slide(C, i);
}
0 1 2 3 4 5 6 7

Correctness proof —

  • Variable: \(i \in \{1 \ldots n\}\)
  • Loop invariant: at start of arb iter \(i\), C is a _______ of the input, and C[0..___] is sorted
  • Initialization: \(i =\) ___ .   C[0..___] sorted?
  • Restoration: assume LI holds at start of iteration \(i\).
  • Termination: \(i =\) _____ , LI gives