Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,

Similar presentations


Presentation on theme: "CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,"— Presentation transcript:

1 CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n, gcd(m, n): if m%n == 0, then gcd(m,n) = n, else gcd(n, m%n); /* iterative version of gcd function */ int GCD(int u, int v){ int temp; while (v!=0){ temp = u % v; u = v; v = temp; } return u; } /* recursive version of gcd function */ int gcd(int m, int n){ int ans; if (m%n == 0) ans = n; else ans = gcd(n, m%n); return ans; }

2 CP104 Introduction to Programming Recursion 2 Lecture 29 __ 2 Recursive Function for Fibonacci Numbers Fibonacci number is defined by recursively as follows. F(1) = F(2) = 1, F(n) = F(n-1) + F(n-2); int fibonacci(int n) { int ans; if (n == 1 || n == 2) ans = 1; else ans = fibonacci(n - 2) + fibonacci(n - 1); return (ans); }

3 CP104 Introduction to Programming Recursion 2 Lecture 29 __ 3 Recursive Algorithm for Josephus Problem Josephus problem was presented in lecture 25. Let J(n) be the last position left with n people sitting around a table. It was found that J(n) has the following relation J(1) = 1, J(n) = 2J(n/2) – 1, if n is an even number J(n) = 2J((n-1)/2) + 1, if n is an odd number int josephus(int n) { if (n == 1) return 1; else if (n%2 == 0) return (2*josephus(n/2)-1); else return ( 2*josephus((n-1)/2)+1 ); } Running time is O(log n).

4 CP104 Introduction to Programming Recursion 2 Lecture 29 __ 4 Brute Force Algorithm for Josephus Problem Int a[n] for (i=0; i<n; ++i) { a[i] = 1; } int Josephus_brute_force(int a[], int n){ int i = 0, j = 0, h = 0; while (j<n-1) { if (a[i] == 1 && h == 1) { a[i] = 0; ++j; h = (h+1)%2; } else if (a[i] == 1 && h != 1) { h = (h+1)%2; } i = (i+1)%n; } for (i = 0; i<n; ++i) { if (a[i] == 1) return i+1; } Running time: the running time for this brute force algorithm is O(nlog n). It is worse than the recursive algorithm O(log n) What about in real running time? See testing. Conclusion: recursive algorithm is far more faster than the brute force algorithm, and the recursive algorithm is much easier to program. But is need more thinking in finding the recursive relation Question: Can we do better?

5 CP104 Introduction to Programming Recursion 2 Lecture 29 __ 5 A More Efficient Algorithm for Josephus Problem 40 10 = 101000 = a[0]a[1]a[2]a[3]a[4]a[5] J(40) = 2(2(2(2(2J(1)-1)+1)-1)-1)-1) J = 1 For i = 1; i<=5; i++ J = 2*J + (-1+2*a[i]); int super_josephus(int b[], int k) { int i, J = 1, ans; if (k == 1) ans = 1; else { for (i = k-2; i>=0; i--) J = 2*J + 2*b[i]-1; ans = J; } return ans; } The running time of this algorithm is O(log n). But it is about 3 times faster than the recursive algorithm

6 CP104 Introduction to Programming Recursion 2 Lecture 29 __ 6 Recursive Selection Sort

7 CP104 Introduction to Programming Recursion 2 Lecture 29 __ 7 Recursive Selection Sort (cont’d)

8 CP104 Introduction to Programming Recursion 2 Lecture 29 __ 8 Trace of Selection Sort

9 CP104 Introduction to Programming Recursion 2 Lecture 29 __ 9 Recursive Binary Search Assume a[0], a[1], … …, a[n] is list of integers in increasing order. Design a recursive binary search function binary_sear(int a[], int x, int left, int right){ int ans; if (left == right && x == a[left]) ans = left; else if ( x != a[left]) ans = -1; else { mid = (left + right)/2; if ( x == a[mid]) ans = mid; else if (x > a[mid]) ans = binary_sear(a, x, mid +1, right); else ans = binary_sear(a, x, left, mid); }


Download ppt "CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,"

Similar presentations


Ads by Google