Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture No 5A Advance Analysis of Institute of Southern Punjab Multan

Similar presentations


Presentation on theme: "Lecture No 5A Advance Analysis of Institute of Southern Punjab Multan"— Presentation transcript:

1 Lecture No 5A Advance Analysis of Institute of Southern Punjab Multan
Department of Computer Science

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 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 0: step 0.

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 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 1: step 0.

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 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 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.

6 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.

7 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.

8 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.

9 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.

10 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.

11 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.

12 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.

13 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.

14 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.

15 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.

16 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.

17 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.

18 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.

19 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.

20 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.

21 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.

22 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.

23 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.

24 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.

25 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.

26 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.

27 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.

28 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.

29 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.

30 5 2 4 6 1 3 Insertion Sort input array
at each iteration, the array is divided in two sub-arrays: left sub-array right sub-array unsorted sorted

31 Insertion Sort

32 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 /22/2019

33 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 /22/2019

34 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 /22/2019

35 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 /22/2019

36 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 /22/2019

37 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 /22/2019

38 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 /22/2019

39 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 /22/2019

40 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 /22/2019

41 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 /22/2019

42 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 /22/2019

43 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 /22/2019

44 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 /22/2019

45 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 /22/2019

46 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 /22/2019

47 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 /22/2019

48 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 /22/2019

49 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 /22/2019

50 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 /22/2019

51 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 /22/2019

52 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 /22/2019

53 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 /22/2019

54 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 /22/2019

55 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 /22/2019

56 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 /22/2019

57 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 /22/2019

58 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 /22/2019

59 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 /22/2019

60 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 /22/2019

61 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 /22/2019

62 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 /22/2019

63 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 /22/2019

64 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 c6(T-(n-1)) } A[j+1] = key c7(n-1) } } T = t2 + t3 + … + tn where ti is number of while expression evaluations for the ith for loop iteration David Luebke /22/2019


Download ppt "Lecture No 5A Advance Analysis of Institute of Southern Punjab Multan"

Similar presentations


Ads by Google