Review Introduction to Recursion Recursive Definition Recursive Algorithms

  • Slides: 86
Download presentation
Review �Introduction to Recursion �Recursive Definition �Recursive Algorithms �Finding a Recursive Solution �Example Recursive

Review �Introduction to Recursion �Recursive Definition �Recursive Algorithms �Finding a Recursive Solution �Example Recursive Function �Recursive Programming �Rules for Recursive Function �Example Tower of Hanoi �Other examples 1

Bubble Sort �Bubble Sort Algorithm �Time Complexity �Best case �Average case �Worst case �Examples

Bubble Sort �Bubble Sort Algorithm �Time Complexity �Best case �Average case �Worst case �Examples 2

Bubble Sort �In bubble sort, each element is compared with its adjacent element �If

Bubble Sort �In bubble sort, each element is compared with its adjacent element �If the first element is larger than the second one, then the positions of the elements are interchanged otherwise it is not changed �Then next element is compared with its adjacent element and the same process is repeated for all the elements in the array until we get a sorted array 3

�Let A be a linear array of n numbers �Suppose the list of numbers

�Let A be a linear array of n numbers �Suppose the list of numbers A[1], A[2], ………… A[n] is an element of array A �Sorting of A means rearranging the elements of A so that they are in order �Here we are dealing with ascending order. i. e. , A[1] < A[2] < A[3]<. . . A[n] �The bubble sort algorithm works as follows: 4

5 Step 1: Compare A[1] and A[2] and arrange them in the ascending order

5 Step 1: Compare A[1] and A[2] and arrange them in the ascending order so that A[1] < A[2] If A[1] is greater than A[2] then interchange the position of data by swap = A[1]; A[1] = A[2]; A[2] = swap Then compare A[2] and A[3] and arrange them so that A[2] < A[3] Continue the process until we compare A[N – 1] with A[N] Step 1 contains n – 1 comparisons i. e. , the largest element is “bubbled up” to the nth position When step 1 is completed A[N] will contain the largest element

�Step 2: Repeat step 1 with one less comparisons that is, now stop comparison

�Step 2: Repeat step 1 with one less comparisons that is, now stop comparison at A [n – 1] and possibly rearrange A[N – 2] and A[N – 1] and so on �In the first pass, step 2 involves n– 2 comparisons and the second largest element will occupy A[n-1] �And in the second pass, step 2 involves n – 3 comparisons and the 3 rd largest element will occupy A[n – 2] and so on �After n – 1 steps, the array will be a sorted array in increasing (or ascending) order 6

Algorithm �Let A be a linear array of n numbers. Swap is a 7

Algorithm �Let A be a linear array of n numbers. Swap is a 7 temporary variable for swapping (or interchange) the position of the numbers 1. Input n numbers of an array A 2. Initialize i = 0 and repeat through step 4 if (i < n) 3. Initialize j = 0 and repeat through step 4 if (j < n – i – 1) 4. If (A[j] > A[j + 1]) (a) Swap = A[j] (b) A[j] = A[j + 1] (c) A[j + 1] = Swap 5. Display the sorted numbers of array A 6. Exit.

Time Complexity �The time complexity for bubble sort is calculated in terms of the

Time Complexity �The time complexity for bubble sort is calculated in terms of the number of comparisons f (n) (or of number of loops) �Here two loops (outer loop and inner loop) iterates (or repeated) the comparisons �The number of times the outer loop iterates is determined by the number of elements in the list which is asked to sort �The inner loop is iterated one less than the number of elements in the list f(n)=n (n-1)=O(n 2) 8

Best Case �In this case the inner loop will iterate with the ‘if’ condition

Best Case �In this case the inner loop will iterate with the ‘if’ condition evaluating time, that is the swap procedure is never called �In best case outer loop will terminate after one iteration, i. e. , it involves performing one pass, which requires n– 1 comparisons �f (n) = O(n) 9

Worst Case �In this case the array will be an inverted list (i. e.

Worst Case �In this case the array will be an inverted list (i. e. , 5, 4, 3, 2, 1, 0) �Here to move first element to the end of the array, n– 1 times the swapping procedure is to be called �Every other element in the list will also move one location towards the start or end of the loop on every iteration �Thus n times the outer loop will iterate and n (n-1) times the inner loop will iterate to sort an inverted array �f(n) = (n(n – 1))/2 = O(n 2) 10

Average Case �Average case is very difficult to analyze than the other cases �In

Average Case �Average case is very difficult to analyze than the other cases �In this case the input data are randomly placed in the list �The exact time complexity can be calculated only if we know the number of iterations, comparisons and swapping �In general, the complexity of average case is: �f(n) = (n(n– 1))/2 = O(n 2) 11

An Example N 8 to_do 7 swap true index 98 1 12 23 45

An Example N 8 to_do 7 swap true index 98 1 12 23 45 14 6 67 33 42 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 1 98 1 13 swap false 23

An Example N 8 to_do 7 index 1 98 1 13 swap false 23 45 14 6 67 33 42 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 1 swap false Swap 14 98 23

An Example N 8 to_do 7 index 1 swap false Swap 14 98 23 45 14 6 67 33 42 1 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 1 true swap Swap 15 23 98

An Example N 8 to_do 7 index 1 true swap Swap 15 23 98 45 14 6 67 33 42 1 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 2 23 1 16 true swap 98

An Example N 8 to_do 7 index 2 23 1 16 true swap 98 45 14 6 67 33 42 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 2 swap true Swap 23 1 17

An Example N 8 to_do 7 index 2 swap true Swap 23 1 17 98 45 14 6 67 33 42 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 2 swap true Swap 18 23 45

An Example N 8 to_do 7 index 2 swap true Swap 18 23 45 98 14 6 67 33 42 1 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 3 23 1 19 swap true 45

An Example N 8 to_do 7 index 3 23 1 19 swap true 45 98 14 6 67 33 42 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 3 swap true Swap 23 1 20

An Example N 8 to_do 7 index 3 swap true Swap 23 1 20 45 2 98 3 4 14 5 6 6 67 7 8 33 42

An Example N 8 to_do 7 index 3 swap true Swap 23 1 21

An Example N 8 to_do 7 index 3 swap true Swap 23 1 21 45 14 98 6 67 33 42 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 4 23 1 22 swap true 45

An Example N 8 to_do 7 index 4 23 1 22 swap true 45 14 98 6 67 33 42 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 4 swap true Swap 23 1 23

An Example N 8 to_do 7 index 4 swap true Swap 23 1 23 45 14 98 6 67 33 42 2 3 4 5 6 7 8

An Example N 8 to_do 7 index 4 swap true Swap 24 23 45

An Example N 8 to_do 7 index 4 swap true Swap 24 23 45 14 1 2 3 6 4 98 67 33 42 5 6 7 8

An Example 25 N 8 to_do 7 index 5 swap 23 45 14 1

An Example 25 N 8 to_do 7 index 5 swap 23 45 14 1 2 3 6 4 true 98 67 33 42 5 6 7 8

An Example N 8 to_do 7 index 5 swap true Swap 23 1 26

An Example N 8 to_do 7 index 5 swap true Swap 23 1 26 45 14 2 3 6 4 98 67 33 42 5 6 7 8

An Example N 8 to_do 7 index 5 swap true Swap 23 1 27

An Example N 8 to_do 7 index 5 swap true Swap 23 1 27 45 14 2 3 6 4 67 98 33 42 5 6 7 8

An Example N 8 to_do 7 index 6 23 1 28 swap 45 14

An Example N 8 to_do 7 index 6 23 1 28 swap 45 14 2 3 6 4 67 5 true 98 33 42 6 7 8

An Example N 8 to_do 7 index 6 swap true Swap 23 1 29

An Example N 8 to_do 7 index 6 swap true Swap 23 1 29 45 14 2 3 6 4 67 5 98 33 42 6 7 8

An Example N 8 to_do 7 index 6 swap true Swap 23 1 30

An Example N 8 to_do 7 index 6 swap true Swap 23 1 30 45 14 2 3 6 4 67 5 33 98 42 6 7 8

An Example N 8 to_do 7 index 7 23 1 31 swap 45 14

An Example N 8 to_do 7 index 7 23 1 31 swap 45 14 2 3 6 4 67 5 true 33 98 42 6 7 8

An Example N 8 to_do 7 index 7 swap true Swap 23 1 32

An Example N 8 to_do 7 index 7 swap true Swap 23 1 32 45 14 2 3 6 4 67 5 33 98 42 6 7 8

An Example N 8 to_do 7 index 7 swap true Swap 23 1 33

An Example N 8 to_do 7 index 7 swap true Swap 23 1 33 45 14 2 3 6 4 67 5 33 42 98 6 7 8

After First Pass of Outer Loop N 8 to_do 7 index 8 23 1

After First Pass of Outer Loop N 8 to_do 7 index 8 23 1 34 swap true Finished first “Bubble Up” 45 14 2 3 6 4 67 5 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 1 23 1 35 swap

The Second “Bubble Up” N 8 to_do 6 index 1 23 1 35 swap 45 14 2 3 6 4 67 5 false 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 1 swap false No Swap

The Second “Bubble Up” N 8 to_do 6 index 1 swap false No Swap 23 1 36 45 14 2 3 6 4 67 5 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 2 23 1 37 swap

The Second “Bubble Up” N 8 to_do 6 index 2 23 1 37 swap 45 14 2 3 6 4 67 5 false 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 2 swap false Swap 23

The Second “Bubble Up” N 8 to_do 6 index 2 swap false Swap 23 1 38 45 14 2 3 6 4 67 5 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 2 swap true Swap 23

The Second “Bubble Up” N 8 to_do 6 index 2 swap true Swap 23 1 39 14 45 2 3 6 4 67 5 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 3 23 1 40 swap

The Second “Bubble Up” N 8 to_do 6 index 3 23 1 40 swap 14 45 2 3 6 4 67 5 true 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 3 swap true Swap 23

The Second “Bubble Up” N 8 to_do 6 index 3 swap true Swap 23 1 41 14 45 2 3 6 4 67 5 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 3 swap true Swap 23

The Second “Bubble Up” N 8 to_do 6 index 3 swap true Swap 23 1 42 14 6 45 67 2 3 4 5 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 4 23 1 43 swap

The Second “Bubble Up” N 8 to_do 6 index 4 23 1 43 swap 14 6 45 67 2 3 4 5 true 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 4 swap true No Swap

The Second “Bubble Up” N 8 to_do 6 index 4 swap true No Swap 23 1 44 14 6 45 67 2 3 4 5 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 5 23 1 45 swap

The Second “Bubble Up” N 8 to_do 6 index 5 23 1 45 swap 14 6 45 67 2 3 4 5 true 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 5 swap true Swap 23

The Second “Bubble Up” N 8 to_do 6 index 5 swap true Swap 23 1 46 14 6 45 67 2 3 4 5 33 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 5 swap true Swap 23

The Second “Bubble Up” N 8 to_do 6 index 5 swap true Swap 23 1 47 14 6 45 33 2 3 4 5 67 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 6 23 1 48 swap

The Second “Bubble Up” N 8 to_do 6 index 6 23 1 48 swap 14 6 45 33 2 3 4 5 true 67 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 6 swap true Swap 23

The Second “Bubble Up” N 8 to_do 6 index 6 swap true Swap 23 1 49 14 6 45 33 2 3 4 5 67 42 98 6 7 8

The Second “Bubble Up” N 8 to_do 6 index 6 swap true Swap 23

The Second “Bubble Up” N 8 to_do 6 index 6 swap true Swap 23 1 50 14 6 45 33 2 3 4 5 42 67 98 6 7 8

After Second Pass of Outer Loop N 8 to_do 6 index 7 23 1

After Second Pass of Outer Loop N 8 to_do 6 index 7 23 1 51 swap true Finished second “Bubble Up” 14 6 45 33 2 3 4 5 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 1 23 1 52 swap

The Third “Bubble Up” N 8 to_do 5 index 1 23 1 52 swap 14 6 45 33 2 3 4 5 false 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 1 swap false Swap 23

The Third “Bubble Up” N 8 to_do 5 index 1 swap false Swap 23 1 53 14 6 45 33 2 3 4 5 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 1 swap true Swap 14

The Third “Bubble Up” N 8 to_do 5 index 1 swap true Swap 14 1 54 23 6 45 33 2 3 4 5 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 2 14 1 55 swap

The Third “Bubble Up” N 8 to_do 5 index 2 14 1 55 swap 23 6 45 33 2 3 4 5 true 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 2 swap true Swap 14

The Third “Bubble Up” N 8 to_do 5 index 2 swap true Swap 14 1 56 23 6 45 33 2 3 4 5 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 2 swap true Swap 57

The Third “Bubble Up” N 8 to_do 5 index 2 swap true Swap 57 14 6 1 2 23 45 33 3 4 5 42 67 98 6 7 8

The Third “Bubble Up” 58 N 8 to_do 5 index 3 swap 14 6

The Third “Bubble Up” 58 N 8 to_do 5 index 3 swap 14 6 1 2 23 45 33 3 4 5 true 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 3 swap true No Swap

The Third “Bubble Up” N 8 to_do 5 index 3 swap true No Swap 59 14 6 1 2 23 45 33 3 4 5 42 67 98 6 7 8

The Third “Bubble Up” 60 N 8 to_do 5 index 4 swap 14 6

The Third “Bubble Up” 60 N 8 to_do 5 index 4 swap 14 6 1 2 23 45 33 3 4 5 true 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 4 swap true Swap 61

The Third “Bubble Up” N 8 to_do 5 index 4 swap true Swap 61 14 6 1 2 23 45 33 3 4 5 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 4 swap true Swap 62

The Third “Bubble Up” N 8 to_do 5 index 4 swap true Swap 62 14 6 1 2 23 33 45 3 4 5 42 67 98 6 7 8

The Third “Bubble Up” 63 N 8 to_do 5 index 5 swap 14 6

The Third “Bubble Up” 63 N 8 to_do 5 index 5 swap 14 6 1 2 23 33 45 3 4 5 true 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 5 swap true Swap 64

The Third “Bubble Up” N 8 to_do 5 index 5 swap true Swap 64 14 6 1 2 23 33 45 3 4 5 42 67 98 6 7 8

The Third “Bubble Up” N 8 to_do 5 index 5 swap true Swap 65

The Third “Bubble Up” N 8 to_do 5 index 5 swap true Swap 65 14 6 1 2 23 33 42 3 4 5 45 67 98 6 7 8

After Third Pass of Outer Loop 66 N 8 to_do 5 index 6 swap

After Third Pass of Outer Loop 66 N 8 to_do 5 index 6 swap true Finished third “Bubble Up” 14 6 1 2 23 33 42 3 4 5 45 67 98 6 7 8

The Fourth “Bubble Up” 67 N 8 to_do 4 index 1 swap 14 6

The Fourth “Bubble Up” 67 N 8 to_do 4 index 1 swap 14 6 1 2 23 33 42 3 4 5 false 45 67 98 6 7 8

The Fourth “Bubble Up” N 8 to_do 4 index 1 swap false Swap 68

The Fourth “Bubble Up” N 8 to_do 4 index 1 swap false Swap 68 14 6 1 2 23 33 42 3 4 5 45 67 98 6 7 8

The Fourth “Bubble Up” N 8 to_do 4 index 1 swap true Swap 6

The Fourth “Bubble Up” N 8 to_do 4 index 1 swap true Swap 6 1 69 14 23 33 42 2 3 4 5 45 67 98 6 7 8

The Fourth “Bubble Up” N 8 to_do 4 index 2 6 1 70 swap

The Fourth “Bubble Up” N 8 to_do 4 index 2 6 1 70 swap 14 23 33 42 2 3 4 5 true 45 67 98 6 7 8

The Fourth “Bubble Up” N 8 to_do 4 index 2 swap true No Swap

The Fourth “Bubble Up” N 8 to_do 4 index 2 swap true No Swap 6 1 71 14 23 33 42 2 3 4 5 45 67 98 6 7 8

The Fourth “Bubble Up” N 8 to_do 4 index 3 6 1 72 swap

The Fourth “Bubble Up” N 8 to_do 4 index 3 6 1 72 swap 14 23 33 42 2 3 4 5 true 45 67 98 6 7 8

The Fourth “Bubble Up” N 8 to_do 4 index 3 swap true No Swap

The Fourth “Bubble Up” N 8 to_do 4 index 3 swap true No Swap 6 1 73 14 23 33 42 2 3 4 5 45 67 98 6 7 8

The Fourth “Bubble Up” N 8 to_do 4 index 4 6 1 74 swap

The Fourth “Bubble Up” N 8 to_do 4 index 4 6 1 74 swap 14 23 33 42 2 3 4 5 true 45 67 98 6 7 8

The Fourth “Bubble Up” N 8 to_do 4 index 4 swap true No Swap

The Fourth “Bubble Up” N 8 to_do 4 index 4 swap true No Swap 6 1 75 14 23 33 42 2 3 4 5 45 67 98 6 7 8

After Fourth Pass of Outer Loop N 8 to_do 4 index 5 6 1

After Fourth Pass of Outer Loop N 8 to_do 4 index 5 6 1 76 swap true Finished fourth “Bubble Up” 14 23 33 42 2 3 4 5 45 67 98 6 7 8

The Fifth “Bubble Up” N 8 to_do 3 index 1 6 1 77 swap

The Fifth “Bubble Up” N 8 to_do 3 index 1 6 1 77 swap 14 23 33 42 2 3 4 5 false 45 67 98 6 7 8

The Fifth “Bubble Up” N 8 to_do 3 index 1 swap false No Swap

The Fifth “Bubble Up” N 8 to_do 3 index 1 swap false No Swap 6 1 78 14 23 33 42 2 3 4 5 45 67 98 6 7 8

The Fifth “Bubble Up” N 8 to_do 3 index 2 6 1 79 swap

The Fifth “Bubble Up” N 8 to_do 3 index 2 6 1 79 swap 14 23 33 42 2 3 4 5 false 45 67 98 6 7 8

The Fifth “Bubble Up” N 8 to_do 3 index 2 swap false No Swap

The Fifth “Bubble Up” N 8 to_do 3 index 2 swap false No Swap 6 1 80 14 23 33 42 2 3 4 5 45 67 98 6 7 8

The Fifth “Bubble Up” N 8 to_do 3 index 3 6 1 81 swap

The Fifth “Bubble Up” N 8 to_do 3 index 3 6 1 81 swap 14 23 33 42 2 3 4 5 false 45 67 98 6 7 8

The Fifth “Bubble Up” N 8 to_do 3 index 3 swap false No Swap

The Fifth “Bubble Up” N 8 to_do 3 index 3 swap false No Swap 6 1 82 14 23 33 42 2 3 4 5 45 67 98 6 7 8

After Fifth Pass of Outer Loop N 8 to_do 3 index 4 6 1

After Fifth Pass of Outer Loop N 8 to_do 3 index 4 6 1 83 swap false Finished fifth “Bubble Up” 14 23 33 42 2 3 4 5 45 67 98 6 7 8

Finished “Early” N 8 to_do 3 index 4 swap false We didn’t do any

Finished “Early” N 8 to_do 3 index 4 swap false We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop. 6 1 84 14 23 33 42 2 3 4 5 45 67 98 6 7 8

More Examples 85

More Examples 85

Summary �Bubble Sort Algorithm �Time Complexity �Best case �Average case �Worst case �Examples 86

Summary �Bubble Sort Algorithm �Time Complexity �Best case �Average case �Worst case �Examples 86