Basic sorting
Selection sort Correctness
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1
Basic sorting Selection sort Correctness January 18, 2019 Cinda - - PowerPoint PPT Presentation
Basic sorting Selection sort Correctness January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1 Queue operations A queue should implement at least the first two of these operations: enqueue insert item at the back of the
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1
– enqueue – insert item at the back of the queue – dequeue – remove an item from the front – peek – return the item at the front of the queue without removing it – isEmpty – check if the queue does not contain any items
– That is, in constant time
Cinda Heeren / Will Evans / Geoffrey Tien 2 January 18, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 3
1 3 2 4 5 6 7 1 2 3 4 5 6 7
January 18, 2019
– 1%5 = 1, 2%5 = 2, 5%5 = 0, 8%5 = 3
– Thereby avoiding comparisons to the array size – The back of the queue is:
– After removing an item, the front of the queue is:
Cinda Heeren / Will Evans / Geoffrey Tien 4 January 18, 2019
Member attributes: int front; int arrlength; int* arr; int num;
Cinda Heeren / Will Evans / Geoffrey Tien 5
Queue q;
front num
q.enqueue(6);
Insert item at (front + num) % queue.length, then increment num 1 1 2 3 4 5
January 18, 2019
6
Cinda Heeren / Will Evans / Geoffrey Tien 6
Queue q;
front num
q.enqueue(6);
Insert item at (front + num) % queue.length, then increment num 6 5
q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8);
4 7 3 8
q.dequeue();
Remove item at front, then decrement num and make front = (front + 1) % queue.length 4 1
q.dequeue();
3 2 1 2 3 4 5
January 18, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 7
front num 2 Insert item at (front + num) % queue.length, then increment num 5 3 7 3 8 Remove item at front, then decrement num and make front = (front + 1) % queue.length 1 2 3 4 5
q.enqueue(9);
9 4
q.enqueue(5);
5 Need to check that the back of the queue does not overtake the front
January 18, 2019
Queue q; q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue(); q.dequeue();
– Then we perform more enqueues to fill the array – How should we resize the array to allow for more enqueue operations?
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 8
12 5 76 33 2 41 ? ? ? ? ? ? ? ? ? ? ? ? front
– So we must avoid traversing through the list – Use a second node pointer to keep track of the node at the back of the queue
Cinda Heeren / Will Evans / Geoffrey Tien 9
Using a Linked List
January 18, 2019
Member attributes: Node* front; Node* back; int num;
Cinda Heeren / Will Evans / Geoffrey Tien 10
Queue q;
front back
q.enqueue(6);
6
q.enqueue(4);
4
q.enqueue(7);
7
q.enqueue(3);
3
q.dequeue();
January 18, 2019
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 11
– How might you organize your hand (algorithmically)?
– The array is divided into a sorted part and an unsorted part
– Starting with the element with index 0, and – Ending with last but one element (index 𝑜 − 1) Selection sort
12 January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 13
23 41 33 81 7 19 11 45 Find smallest unsorted item: 7 comparisons 7 41 33 81 23 19 11 45 Find smallest unsorted item: 6 comparisons 7 11 33 81 23 19 41 45 Find smallest unsorted item: 5 comparisons 7 11 19 81 23 33 41 45 Find smallest unsorted item: 4 comparisons 7 11 19 23 81 33 41 45 Find smallest unsorted item: 3 comparisons 7 11 19 23 33 81 41 45 Find smallest unsorted item: 2 comparisons 7 11 19 23 33 41 81 45 Find smallest unsorted item: 1 comparison 7 11 19 23 33 41 45 81 Sorted
14
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien
15
void SelectionSort(vector<int>& arr) { for (int i = 0; i < arr.size() -1; ++i) { int smallest = i; // Find the index of the smallest element for (int j = i + 1; j < arr.size(); ++j) { if (arr[j] < arr[smallest]) { smallest = j; } } // Swap the smallest with the current item temp = arr[i];{ arr[i] = arr[smallest]; arr[smallest] = temp; } }
Outer loop: 𝑜 times Inner loop: 𝑜 − 𝑗 times each, 𝑜 𝑜 − 1 2 times total
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien
– Makes 𝑜 ∗ 𝑜 − 1 2 comparisons, regardless of the original order of the input – Performs 𝑜 − 1 swaps
– In asymptotic terms, how many swaps are done? – This might work in extremely limited systems where reads are cheap, but writes are expensive
16 January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien
– Properties of the algorithm or structure that are always true (i.e. do not vary) at particular points in the program – Within a loop body, these properties may become violated briefly, but the rest of the loop instructions should fix the properties for the next iteration
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 17
Proving algorithm correctness using loop invariants
– use loop invariant to rigourously prove that this happens
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 18
Inner loop
int smallest = i; // Find the index of the smallest element for (int j = i + 1; j < arr.size(); ++j) { if (arr[j] < arr[smallest]) smallest = j; }
7 ... 2 12 4 9 6 5 8 3 16 smallest:
14 15 16 17 18 19 20 21 22
𝑗 𝑘
– Before iteration 𝑘 of the inner loop, smallest contains the index of the smallest element of 𝑏𝑠𝑠 𝑗. . 𝑘 − 1 – Plain English version: smallest contains the index of the smallest element seen so far – Use induction to build up towards loop termination, where "so far" is the entire unordered subarray
– Before the first iteration of the inner loop, the invariant states that smallest contains the index of the smallest element of 𝑏𝑠𝑠 𝑗. . 𝑗 – This is set explicitly by the line int smallest = i; – Therefore the loop invariant holds in the base case
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 19
Inner loop correctness
– Before iteration 𝑘 of the inner loop, smallest contains the index of the smallest element of 𝑏𝑠𝑠 𝑗. . 𝑘 − 1
– Assume loop invariant holds prior to processing index 𝑙, i.e. smallest contains the index of the smallest element of 𝑏𝑠𝑠 𝑗. . 𝑙 − 1 – Case 1: arr[k] < arr[smallest]
𝑏𝑠𝑠 𝑗. . 𝑙 , thus loop invariant holds after processing index 𝑙
– Case 2: arr[k] ≮ arr[smallest]
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 20
Inner loop correctness
– Before iteration 𝑘 of the inner loop, smallest contains the index of the smallest element of 𝑏𝑠𝑠 𝑗. . 𝑘 − 1
– Invariant property states that smallest contains the index of the smallest element of 𝑏𝑠𝑠 𝑗. . 𝑏𝑠𝑠. 𝑡𝑗𝑨𝑓() − 1 – This range describes the entire unordered subarray – Therefore at termination, smallest contains the index of the smallest element in the entire unordered subarray
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 21
Inner loop correctness
Now that we know the inner loop works properly, we can prove that the outer loop works properly
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 22
Outer loop correctness 2 3 5 6 8 14 25 18 23 10 20 16
1 2 3 4 5 6 7 8 9 10 11
𝑗
– Before iteration 𝑗 of the outer loop, 𝑏𝑠𝑠 0. . 𝑗 − 1 contains the 𝑗 smallest elements of arr in ascending order
Also important to note 𝑏𝑠𝑠 𝑗. . 𝑏𝑠𝑠. 𝑡𝑗𝑨𝑓() − 1 contains the arr. size() − 𝑗 largest elements of arr, unordered, but we will focus on the ordered subarray
– Before iteration 𝑗 of the outer loop, 𝑏𝑠𝑠 0. . 𝑗 − 1 contains the 𝑗 smallest elements of arr in ascending order
– Before executing the iteration for 𝑗 = 0, the invariant states that 𝑏𝑠𝑠 0. . −1 contains the 0 smallest elements of arr in ascending order – Since the interval 0. . −1 contains no elements, the invariant holds in the base case
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 23
Outer loop correctness
– Before iteration 𝑗 of the outer loop, 𝑏𝑠𝑠 0. . 𝑗 − 1 contains the 𝑗 smallest elements of arr in ascending order
– Assume the invariant holds before processing index k, i.e. 𝑏𝑠𝑠 0. . 𝑙 − 1 contains the 𝑙 smallest elements of arr in ascending order – The inner loop correctly identifies the index of the smallest item from 𝑏𝑠𝑠 𝑙. . 𝑏𝑠𝑠. 𝑡𝑗𝑨𝑓() − 1
– This item swaps with arr[k] – Thus after processing index k, 𝑏𝑠𝑠 0. . 𝑙 contains the 𝑙 + 1 smallest elements of arr in ascending order and the loop invariant holds
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 24
Outer loop correctness
Why?
– Before iteration 𝑗 of the outer loop, 𝑏𝑠𝑠 0. . 𝑗 − 1 contains the 𝑗 smallest elements of arr in ascending order
– The invariant states that 𝑏𝑠𝑠 0. . 𝑏𝑠𝑠. 𝑡𝑗𝑨𝑓() − 2 contains the 𝑏𝑠𝑠. 𝑡𝑗𝑨𝑓() − 1 smallest elements of arr in ascending order – 𝑏𝑠𝑠[𝑏𝑠𝑠. 𝑡𝑗𝑨𝑓() − 1] contains a single element at least as large as all elements in 𝑏𝑠𝑠 0. . 𝑏𝑠𝑠. 𝑡𝑗𝑨𝑓() − 2
covers the complete array
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 25
Outer loop correctness
– Chapter 11.1.1 (Selection sort)
– Chapter 5.5 (Loop invariants, correctness)
– Carrano & Henry: Chapter 11.1.3 (Insertion sort)
January 18, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 26