analysis of insertion sort - inf.usi.ch · insertion sort idea: it is like sorting a hand of cards...
TRANSCRIPT
![Page 1: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/1.jpg)
Analysis of Insertion Sort
Antonio Carzaniga
Faculty of InformaticsUniversità della Svizzera italiana
February 27, 2020
![Page 2: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/2.jpg)
Outline
Sorting
Insertion Sort
Analysis
![Page 3: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/3.jpg)
Sorting
Input: a sequence A = 〈a1, a2, . . . , an〉
![Page 4: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/4.jpg)
Sorting
Input: a sequence A = 〈a1, a2, . . . , an〉
Output: a sequence 〈b1, b2, . . . , bn〉 such that
◮ 〈b1, b2, . . . , bn〉 is a permutation of 〈a1, a2, . . . , an〉
![Page 5: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/5.jpg)
Sorting
Input: a sequence A = 〈a1, a2, . . . , an〉
Output: a sequence 〈b1, b2, . . . , bn〉 such that
◮ 〈b1, b2, . . . , bn〉 is a permutation of 〈a1, a2, . . . , an〉
◮ 〈b1, b2, . . . , bn〉 is sorted
b1 ≤ b2 ≤ · · · ≤ bn
![Page 6: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/6.jpg)
Sorting
Input: a sequence A = 〈a1, a2, . . . , an〉
Output: a sequence 〈b1, b2, . . . , bn〉 such that
◮ 〈b1, b2, . . . , bn〉 is a permutation of 〈a1, a2, . . . , an〉
◮ 〈b1, b2, . . . , bn〉 is sorted
b1 ≤ b2 ≤ · · · ≤ bn
Typically, A is implemented as an array
![Page 7: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/7.jpg)
Sorting
Input: a sequence A = 〈a1, a2, . . . , an〉
Output: a sequence 〈b1, b2, . . . , bn〉 such that
◮ 〈b1, b2, . . . , bn〉 is a permutation of 〈a1, a2, . . . , an〉
◮ 〈b1, b2, . . . , bn〉 is sorted
b1 ≤ b2 ≤ · · · ≤ bn
Typically, A is implemented as an array
A = 6 8 3 2 7 6 11 5 9 4
![Page 8: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/8.jpg)
Sorting
Input: a sequence A = 〈a1, a2, . . . , an〉
Output: a sequence 〈b1, b2, . . . , bn〉 such that
◮ 〈b1, b2, . . . , bn〉 is a permutation of 〈a1, a2, . . . , an〉
◮ 〈b1, b2, . . . , bn〉 is sorted
b1 ≤ b2 ≤ · · · ≤ bn
Typically, A is implemented as an array
A = 2 3 4 5 6 6 7 8 9 11
in-place sort
![Page 9: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/9.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
![Page 10: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/10.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
![Page 11: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/11.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 6 8 3 2 7 6 11 5 9 4
![Page 12: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/12.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 6 8 3 2 7 6 11 5 9 4
![Page 13: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/13.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 6 8 3 2 7 6 11 5 9 4
![Page 14: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/14.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 6 8 3 2 7 6 11 5 9 4
![Page 15: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/15.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 6 3 8 2 7 6 11 5 9 4
![Page 16: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/16.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 3 6 8 2 7 6 11 5 9 4
![Page 17: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/17.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 3 6 8 2 7 6 11 5 9 4
![Page 18: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/18.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 3 6 2 8 7 6 11 5 9 4
![Page 19: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/19.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 3 2 6 8 7 6 11 5 9 4
![Page 20: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/20.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 8 7 6 11 5 9 4
![Page 21: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/21.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 8 7 6 11 5 9 4
![Page 22: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/22.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 7 8 6 11 5 9 4
![Page 23: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/23.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 7 8 6 11 5 9 4
![Page 24: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/24.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 7 6 8 11 5 9 4
![Page 25: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/25.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 6 7 8 11 5 9 4
![Page 26: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/26.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 6 7 8 11 5 9 4
![Page 27: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/27.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 6 7 8 11 5 9 4
![Page 28: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/28.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 6 7 8 5 11 9 4
![Page 29: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/29.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 6 7 5 8 11 9 4
![Page 30: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/30.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 6 5 7 8 11 9 4
![Page 31: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/31.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 6 5 6 7 8 11 9 4
![Page 32: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/32.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 6 6 7 8 11 9 4
![Page 33: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/33.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 6 6 7 8 11 9 4
![Page 34: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/34.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 6 6 7 8 9 11 4
![Page 35: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/35.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 6 6 7 8 9 11 4
![Page 36: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/36.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 6 6 7 8 9 4 11
![Page 37: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/37.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 6 6 7 8 4 9 11
![Page 38: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/38.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 6 6 7 4 8 9 11
![Page 39: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/39.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 6 6 4 7 8 9 11
![Page 40: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/40.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 6 4 6 7 8 9 11
![Page 41: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/41.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 5 4 6 6 7 8 9 11
![Page 42: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/42.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 4 5 6 6 7 8 9 11
![Page 43: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/43.jpg)
Insertion Sort
Idea: it is like sorting a hand of cards
◮ scan the sequence left to right
◮ pick the value at the current position aj
◮ insert it in its correct position in the sequence 〈a1, a2, . . . aj−1〉 so as to maintain asorted subsequence 〈a1, a2, . . . aj〉
A = 2 3 4 5 6 6 7 8 9 11
![Page 44: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/44.jpg)
Insertion Sort (2)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
![Page 45: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/45.jpg)
Insertion Sort (2)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Is INSERTION-SORT correct?
What is the time complexity of INSERTION-SORT?
Can we do better?
![Page 46: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/46.jpg)
Complexity of INSERTION-SORT
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
![Page 47: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/47.jpg)
Complexity of INSERTION-SORT
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Outer loop (lines 1–5) runs exactly n − 1 times (with n = length(A))
What about the inner loop (lines 3–5)?
◮ best, worst, and average case?
![Page 48: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/48.jpg)
Complexity of INSERTION-SORT (2)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Best case:
![Page 49: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/49.jpg)
Complexity of INSERTION-SORT (2)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Best case: the inner loop is never executed
◮ what case is this?
![Page 50: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/50.jpg)
Complexity of INSERTION-SORT (2)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Best case: the inner loop is never executed
◮ what case is this?
Worst case:
![Page 51: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/51.jpg)
Complexity of INSERTION-SORT (2)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Best case: the inner loop is never executed
◮ what case is this?
Worst case: the inner loop is executed exactly j − 1 times for every iteration ofthe outer loop
◮ what case is this?
![Page 52: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/52.jpg)
Complexity of INSERTION-SORT (3)
The worst-case complexity is when the inner loop is executed exactly j − 1times, so
T(n) =
n∑
j=2
(j − 1)
![Page 53: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/53.jpg)
Complexity of INSERTION-SORT (3)
The worst-case complexity is when the inner loop is executed exactly j − 1times, so
T(n) =
n∑
j=2
(j − 1)
T(n) is the arithmetic series∑n−1
k=1 k, so
T(n) =n(n − 1)
2
T(n) = Θ(n2)
![Page 54: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/54.jpg)
Complexity of INSERTION-SORT (3)
The worst-case complexity is when the inner loop is executed exactly j − 1times, so
T(n) =
n∑
j=2
(j − 1)
T(n) is the arithmetic series∑n−1
k=1 k, so
T(n) =n(n − 1)
2
T(n) = Θ(n2)
Best-case is T(n) = Θ(n)
![Page 55: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/55.jpg)
Complexity of INSERTION-SORT (3)
The worst-case complexity is when the inner loop is executed exactly j − 1times, so
T(n) =
n∑
j=2
(j − 1)
T(n) is the arithmetic series∑n−1
k=1 k, so
T(n) =n(n − 1)
2
T(n) = Θ(n2)
Best-case is T(n) = Θ(n)
Average-case is T(n) = Θ(n2)
![Page 56: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/56.jpg)
Correctness
![Page 57: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/57.jpg)
Correctness
Does INSERTION-SORT terminate for all valid inputs?
![Page 58: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/58.jpg)
Correctness
Does INSERTION-SORT terminate for all valid inputs?
If so, does it satisfy the conditions of the sorting problem?
◮ A contains a permutation of the initial value of A
◮ A is sorted: A[1] ≤ A[2] ≤ · · · ≤ A[length(A)]
![Page 59: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/59.jpg)
Correctness
Does INSERTION-SORT terminate for all valid inputs?
If so, does it satisfy the conditions of the sorting problem?
◮ A contains a permutation of the initial value of A
◮ A is sorted: A[1] ≤ A[2] ≤ · · · ≤ A[length(A)]
We want a formal proof of correctness
◮ does not seem straightforward. . .
![Page 60: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/60.jpg)
The Logic of Algorithmic Steps
![Page 61: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/61.jpg)
The Logic of Algorithmic Steps
Example 1: (straight-line program)
BIGGER(n)
1 //must return a value greater than n2 m = n ∗ n + 13 returnm
![Page 62: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/62.jpg)
The Logic of Algorithmic Steps
Example 1: (straight-line program)
BIGGER(n)
1 //must return a value greater than n2 m = n ∗ n + 13 returnm
Example 2: (branching)
SORTTWO(A)
1 //must sort (in-place) an array of 2 elements2 if A[1] > A[2]3 t = A[1]4 A[1] = A[2]5 A[2] = t
![Page 63: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/63.jpg)
Loop Invariants
![Page 64: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/64.jpg)
Loop Invariants
We formulate a loop-invariant condition C
◮ C must remain true through a loop
![Page 65: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/65.jpg)
Loop Invariants
We formulate a loop-invariant condition C
◮ C must remain true through a loop
◮ C is relevant to the problem definition: we use C at the end of a loop to prove thecorrectness of the result
![Page 66: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/66.jpg)
Loop Invariants
We formulate a loop-invariant condition C
◮ C must remain true through a loop
◮ C is relevant to the problem definition: we use C at the end of a loop to prove thecorrectness of the result
Then, we only need to prove that the algorithm terminates
![Page 67: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/67.jpg)
Loop Invariants (2)
![Page 68: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/68.jpg)
Loop Invariants (2)
Formulation: this is where we try to be smart
◮ the invariant must reflect the structure of the algorithm
◮ it must be the basis to prove the correctness of the solution
![Page 69: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/69.jpg)
Loop Invariants (2)
Formulation: this is where we try to be smart
◮ the invariant must reflect the structure of the algorithm
◮ it must be the basis to prove the correctness of the solution
Proof of validity (i.e., that C is indeed a loop invariant): typical proof by induction
◮ initialization: we must prove that
the invariant C is true before entering the loop
◮ maintenance: we must prove that
if C is true at the beginning of a cycle then it remains true after one cycle
![Page 70: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/70.jpg)
Loop Invariant for INSERTION-SORT
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
![Page 71: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/71.jpg)
Loop Invariant for INSERTION-SORT
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
The main idea is to insert A[i] in A[1 . . i − 1] so as to maintain a sortedsubsequence A[1 . . i]
![Page 72: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/72.jpg)
Loop Invariant for INSERTION-SORT
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
The main idea is to insert A[i] in A[1 . . i − 1] so as to maintain a sortedsubsequence A[1 . . i]
Invariant: (outer loop) the subarray A[1 . . i − 1] consists of the elements originallyin A[1 . . i − 1] in sorted order
![Page 73: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/73.jpg)
Loop Invariant for INSERTION-SORT (2)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
![Page 74: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/74.jpg)
Loop Invariant for INSERTION-SORT (2)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Initialization: j = 2, so A[1 . . j − 1] is the single element A[1]
◮ A[1] contains the original element in A[1]
◮ A[1] is trivially sorted
![Page 75: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/75.jpg)
Loop Invariant for INSERTION-SORT (3)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
![Page 76: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/76.jpg)
Loop Invariant for INSERTION-SORT (3)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Maintenance: informally, if A[1 . . i − 1] is a permutation of the originalA[1 . . i − 1] and A[1 . . i − 1] is sorted (invariant), then if we enter the inner loop:
◮ shifts the subarray A[k . . i − 1] by one position to the right
◮ inserts key, which was originally in A[i] at its proper position 1 ≤ k ≤ i − 1, insorted order
![Page 77: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/77.jpg)
Loop Invariant for INSERTION-SORT (4)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
![Page 78: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/78.jpg)
Loop Invariant for INSERTION-SORT (4)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Termination: INSERTION-SORT terminates with i = length(A) + 1; the invariantstates that
![Page 79: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/79.jpg)
Loop Invariant for INSERTION-SORT (4)
INSERTION-SORT(A)
1 for i = 2 to length(A)2 j = i3 while j > 1 and A[j − 1] > A[j]4 swap A[j] and A[j − 1]5 j = j − 1
Termination: INSERTION-SORT terminates with i = length(A) + 1; the invariantstates that
◮ A[1 . . i − 1] is a permutation of the original A[1 . . . i − 1]
◮ A[1 . . i − 1] is sorted
Given the termination condition, A[1 . . i − 1] is the whole A
So INSERTION-SORT is correct!
![Page 80: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/80.jpg)
Summary
You are given a problem P and an algorithm A
◮ P formally defines a correctness condition
◮ assume, for simplicity, that A consists of one loop
![Page 81: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/81.jpg)
Summary
You are given a problem P and an algorithm A
◮ P formally defines a correctness condition
◮ assume, for simplicity, that A consists of one loop
1. Formulate an invariant C
![Page 82: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/82.jpg)
Summary
You are given a problem P and an algorithm A
◮ P formally defines a correctness condition
◮ assume, for simplicity, that A consists of one loop
1. Formulate an invariant C
2. Initialization (for all valid inputs)
◮ prove that C holds right before the first execution of the first instruction of the loop
![Page 83: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/83.jpg)
Summary
You are given a problem P and an algorithm A
◮ P formally defines a correctness condition
◮ assume, for simplicity, that A consists of one loop
1. Formulate an invariant C
2. Initialization (for all valid inputs)
◮ prove that C holds right before the first execution of the first instruction of the loop
3. Management (for all valid inputs)
◮ prove that if C holds right before the first instruction of the loop, then it holds alsoat the end of the loop
![Page 84: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/84.jpg)
Summary
You are given a problem P and an algorithm A
◮ P formally defines a correctness condition
◮ assume, for simplicity, that A consists of one loop
1. Formulate an invariant C
2. Initialization (for all valid inputs)
◮ prove that C holds right before the first execution of the first instruction of the loop
3. Management (for all valid inputs)
◮ prove that if C holds right before the first instruction of the loop, then it holds alsoat the end of the loop
4. Termination (for all valid inputs)
◮ prove that the loop terminates, with some exit condition X
![Page 85: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/85.jpg)
Summary
You are given a problem P and an algorithm A
◮ P formally defines a correctness condition
◮ assume, for simplicity, that A consists of one loop
1. Formulate an invariant C
2. Initialization (for all valid inputs)
◮ prove that C holds right before the first execution of the first instruction of the loop
3. Management (for all valid inputs)
◮ prove that if C holds right before the first instruction of the loop, then it holds alsoat the end of the loop
4. Termination (for all valid inputs)
◮ prove that the loop terminates, with some exit condition X
5. Prove that X ∧ C ⇒ P, which means that A is correct
![Page 86: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/86.jpg)
Exercise: Analyze Selection-Sort
SELECTION-SORT(A)
1 n = length(A)2 for i = 1 to n − 13 smallest = i4 for j = i + 1 to n5 if A[j] < A[smallest]6 smallest = j7 swap A[i] and A[smallest]
![Page 87: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/87.jpg)
Exercise: Analyze Selection-Sort
SELECTION-SORT(A)
1 n = length(A)2 for i = 1 to n − 13 smallest = i4 for j = i + 1 to n5 if A[j] < A[smallest]6 smallest = j7 swap A[i] and A[smallest]
Correctness?
◮ loop invariant?
Complexity?
◮ worst, best, and average case?
![Page 88: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/88.jpg)
Exercise: Analyze Bubblesort
BUBBLESORT(A)
1 for i = 1 to length(A)2 for j = length(A) downto i + 13 if A[j] < A[j − 1]4 swap A[j] and A[j − 1]
![Page 89: Analysis of Insertion Sort - inf.usi.ch · Insertion Sort Idea: it is like sorting a hand of cards scan the sequence left to right pick the value at the current position aj insert](https://reader035.vdocuments.us/reader035/viewer/2022081607/5ede3b7dad6a402d66698c51/html5/thumbnails/89.jpg)
Exercise: Analyze Bubblesort
BUBBLESORT(A)
1 for i = 1 to length(A)2 for j = length(A) downto i + 13 if A[j] < A[j − 1]4 swap A[j] and A[j − 1]
Correctness?
◮ loop invariant?
Complexity?
◮ worst, best, and average case?