CPSC 221

Selection Sort

Today

Announcements

  • HW1 is due tonight, 11:59 pm
  • PA1 is out tonight, due Oct 5
  • Examlet 1 is this week — Thursday through Sunday. Book your seat on PrairieTest, and work PEX1
  • PEX2 releases Wednesday night


Where we’re going

  • Warm-up
  • Selection sort — what it does, how long it takes
  • Selection sort — why it works

Warm-up

My algorithm runs in time \(\Omega(n \log n)\).

Your algorithm runs in time \(O(n!)\).


Whose is better?

Where we left off

int idxOfMin (vector<int>& C, int a) {
    int retloc = a;
    for (int i = a+1; i < C.size(); i++)
        if (C[i] < C[retloc]) {
            retloc = i;
        }
    return retloc;
}

We named it, evaluated its runtime, and proved it is correct.

Another example

void ____________ (vector<int>& C) {
    for (int i = 0; i < C.size(); i++) {
        int m = idxOfMin(C, i);
        swap(C, m, i);
    }
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
32 11 73 21 29 86 81 92 57 61 64 15 79 44 7 45

What does it do?    How long does it take?

Selection sort

void selectionSort (vector<int>& C) {
    for (int i = 0; i < C.size(); i++) {
        int m = idxOfMin(C, i);
        swap(C, m, i);
    }
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
7 11 15 21 29 86 81 92 57 61 64 73 79 44 32 45

\(T(n) = \Theta(n^2)\)

Oops

void selectionSort (vector<int>& C) {
    for (int i = 0; i < C.size(); i++) {
        int m = idxOfMin(C, i);
        swap(C, m, i);
    }
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
7 11 15 21 29 86 81 92 57 61 64 73 79 44 18 45

My code crashed partway through the loop.

What is the latest iteration I could have been on?

What does it do?

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

Thm.

How long does it take?

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

What is always true?

At the start of iteration \(i\):

C[0..i-1] is sorted.


Is that enough?

Strengthen it

At the start of iteration \(i\):   C is a permutation of what we started with, C[0..i-1] is sorted, and holds the \(i\) smallest values in C.

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);
    }
}

\(i = 0\):

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:


\(m =\)       after the swap:

Restoration, continued

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);
    }
}

Why is C[0..i] still sorted?


Why does it still hold the \(i+1\) smallest?

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?