1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array...

8
1 Object-Oriented Programming Using C++ CLASS 2

Transcript of 1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array...

Page 1: 1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.

1

Object-Oriented Programming

Using C++

CLASS 2

Page 2: 1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.

2

Linear Recursion

• Summing the Elements of an Array Recursively

• Algorithm LinearSum(A, n):

Input: An integer array A and int n

Output: The sum of the first n int in A

if n = 1 then

return A[0]

else

return LinearSum(A, n-1) + A[n-1]

Page 3: 1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.

3

Linear Recursion

• Reverse an Array by Recursion

• Algorithm ReverseArray(A, i, n):

Input: An integer array A and int i, n

Output: The reversal of n integers in A starting at index i

if n > 1 then

Swap A[i] and A[i+n-1]

Call ReverseArray(A, i+1, n-2)

return

Page 4: 1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.

4

Linear Recursion

• Computing Powers

• Algorithm Power(x, n):

Input: The number x and int n >= 0

Output: The value x power n

if n = 0 then

return 1

if n is odd then

y Power(x, (n-1)/2)

return x * y * y

else

y Power(x, n/2)

return y * y

Page 5: 1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.

5

Tail recursion

• Using recursion can often be a useful tool for designing algorithms, but this usefulness does come at a modest cost

• Whenever we use recursion function to solve a problem, we have to use some of the memory locations in our computer to keep track of the state of each active recursive call

• We can use the stack data structure to convert a recursive algorithm into a non-recursive algorithm, easily the ones which use tail recursion

• A tail recursion is when the algorithm uses linear recursion and the algorithm only makes a recursive call as its last operation. For example, ReverseArray() uses tail recursion and LinearSum() does not

Page 6: 1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.

6

Linear Recursion

• Reverse an Array by Iterative non-Recursion

• Algorithm IterativeReverseArray(A, i, n):

Input: An integer array A and int i, n

Output: The reversal of n integers in A starting at index i

while n > 1 do

Swap A[i] and A[i+n-1]

i i + 1

n n - 2

return

Page 7: 1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.

7

Higher order recursion

• When an instance of recursion algorithm makes more than a single recursive call

• Algorithm BinarySum(A, i, n):

Input: An int array A and int i and n

Output: The sum of the n integers in A starting at I

if n = 1 then

return A[i]

return BinarySum(A, i, [n/2]) +

BinarySum(A, i, [n/2], [n/2])

Page 8: 1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.

8

Higher order recursion

• When an instance of recursion algorithm makes more than a single recursive call

• Algorithm BinaryFib(k)

Input: An int k

Output: The kth Fibonacci number

if k <= 1 then

return k

else

return BinaryFib(k-1) + BinaryFib(k-2)