Lecture No 5A Advance Analysis of Institute of Southern Punjab Multan Department of Computer Science
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 0: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 1: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 2.78 7.42 0.56 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 2: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 2.78 2.78 0.56 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 2: step 1.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 2.78 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 2: step 2.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 2.78 1.12 7.42 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 3: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 2.78 1.12 2.78 1.12 7.42 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 3: step 1.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 2.78 7.42 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 3: step 2.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 2.78 7.42 1.17 7.42 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 4: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 1.17 2.78 2.78 1.17 7.42 0.32 6.21 4.42 3.14 7.71 Iteration 4: step 1.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 1.17 2.78 7.42 0.32 6.21 4.42 3.14 7.71 Iteration 4: step 2.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 1.17 2.78 7.42 0.32 7.42 0.32 6.21 4.42 3.14 7.71 Iteration 5: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 1.17 0.32 2.78 2.78 0.32 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 1.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 0.32 1.17 1.17 0.32 2.78 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 2.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 0.32 1.12 1.12 0.32 1.17 2.78 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 3.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 0.56 0.32 1.12 1.17 2.78 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 4.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 5.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 6.21 7.42 7.42 6.21 4.42 3.14 7.71 Iteration 6: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 6.21 7.42 4.42 3.14 7.71 Iteration 6: step 1.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 6.21 7.42 4.42 7.42 4.42 3.14 7.71 Iteration 7: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 6.21 4.42 7.42 3.14 7.71 Iteration 7: step 1.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 7.42 3.14 7.71 Iteration 7: step 2.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 7.42 3.14 7.42 3.14 7.71 Iteration 8: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 3.14 6.21 3.14 7.42 7.71 Iteration 8: step 1.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 4.42 3.14 6.21 7.42 7.71 Iteration 8: step 2.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71 Iteration 8: step 3.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71 Iteration 9: step 0.
5/22/2019 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71 Iteration 10: DONE.
5 2 4 6 1 3 Insertion Sort input array 5 2 4 6 1 3 at each iteration, the array is divided in two sub-arrays: left sub-array right sub-array unsorted sorted
Insertion Sort
An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 32 5/22/2019
An Example: Insertion Sort 30 10 40 20 i = j = key = A[j] = A[j+1] = 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 33 5/22/2019
An Example: Insertion Sort 30 10 40 20 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 10 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 34 5/22/2019
An Example: Insertion Sort 30 30 40 20 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 30 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 35 5/22/2019
An Example: Insertion Sort 30 30 40 20 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 30 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 36 5/22/2019
An Example: Insertion Sort 30 30 40 20 i = 2 j = 0 key = 10 A[j] = A[j+1] = 30 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 37 5/22/2019
An Example: Insertion Sort 30 30 40 20 i = 2 j = 0 key = 10 A[j] = A[j+1] = 30 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 38 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 2 j = 0 key = 10 A[j] = A[j+1] = 10 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 39 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 3 j = 0 key = 10 A[j] = A[j+1] = 10 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 40 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 3 j = 0 key = 40 A[j] = A[j+1] = 10 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 41 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 3 j = 0 key = 40 A[j] = A[j+1] = 10 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 42 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 43 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 44 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 45 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 4 j = 2 key = 40 A[j] = 30 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 46 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 47 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 48 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 20 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 49 5/22/2019
An Example: Insertion Sort 10 30 40 20 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 20 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 50 5/22/2019
An Example: Insertion Sort 10 30 40 40 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 51 5/22/2019
An Example: Insertion Sort 10 30 40 40 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 52 5/22/2019
An Example: Insertion Sort 10 30 40 40 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 53 5/22/2019
An Example: Insertion Sort 10 30 40 40 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 54 5/22/2019
An Example: Insertion Sort 10 30 40 40 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 55 5/22/2019
An Example: Insertion Sort 10 30 30 40 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 30 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 56 5/22/2019
An Example: Insertion Sort 10 30 30 40 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 30 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 57 5/22/2019
An Example: Insertion Sort 10 30 30 40 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 30 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 58 5/22/2019
An Example: Insertion Sort 10 30 30 40 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 30 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 59 5/22/2019
An Example: Insertion Sort 10 20 30 40 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 20 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } David Luebke 60 5/22/2019
An Example: Insertion Sort 10 20 30 40 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 20 1 2 3 4 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Done! David Luebke 61 5/22/2019
What is the precondition for this loop? Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } What is the precondition for this loop? David Luebke 62 5/22/2019
How many times will this loop execute? Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } How many times will this loop execute? David Luebke 63 5/22/2019
Statement Effort Insertion Sort InsertionSort(A, n) { for i = 2 to n { c1n key = A[i] c2(n-1) j = i - 1; c3(n-1) while (j > 0) and (A[j] > key) { c4T A[j+1] = A[j] c5(T-(n-1)) j = j - 1 c6(T-(n-1)) } 0 A[j+1] = key c7(n-1) } 0 } T = t2 + t3 + … + tn where ti is number of while expression evaluations for the ith for loop iteration David Luebke 64 5/22/2019