Recursion 3
Recursion 3
Recursion 2
In this module, we are going to understand how to solve different kinds of
problems using recursion.
Recursion in strings is not a very different logic, it is the same as we apply in arrays,
in fact it becomes more easy to pass a complete new string using substring
method.
You are given a string of size n containing characters a-z. The task is to write
a recursive function to replace all occurrences of pi with 3.14 in the given
string and print the modified string.
Approach
1. Step of the trivial case: In this step, we will prove the desired statement for
a base case like size of string = 0 or 1.
2. Small calculation and recursive part interlinked: In this step, you will first
check character at 0th index and character at 1st index of the string.
- If it comes out to be ‘p’ and ‘i’ then we make a recursive call passing the
string from index 2. And thereafter “3.14” needs to be concatenated
with the recursive answer and this updated string will be the answer.
1
- Else we will just make a recursive call passing the string from index 1.
Thereafter the character at 0th index needs to be concatenated with
the recursive answer and return the same.
are already sorted by ignoring half of the elements after just one comparison.
You are given a target element X and a sorted array. You need to check if X is
2
Pseudo-Code
public static void mergeSort(arr[], l, r){
if (r > l){
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for the first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for the second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3
:
Call merge(arr, l, m, r)
}
}
The following diagram shows the complete merge sort process for an example
array [
38,27,43,3,9,82,10]. If we take a closer look at the diagram, we can see
3
that the array is recursively divided into two halves till the size becomes 1. Once the
size becomes 1, the merge processes come into action and start merging arrays
back till the complete array is merged.
Quick Sort
Quick-sort is based on the d
ivide-and-conquer approach. It works along the lines
of choosing one element as a pivot element and partitioning the array around it
such that:
● The left side of the pivot contains all the elements that are less than the pivot
element
4
● The right side contains all elements greater than the pivot.
● Divide: T
he array is divided into subparts taking pivot as the partitioning
point. The elements smaller than the pivot are placed to the left of the pivot
and the elements greater than the pivot are placed to the right side.
● Conquer: T
he left and right sub-parts are again partitioned using the by
selecting pivot elements for them. This can be achieved by recursively
passing the subparts into the algorithm.
● Combine: T
his part does not play a significant role in quicksort. The array is
already sorted at the end of the conquer step.
The advantage of quicksort over merge sort is that it does not require any extra
space to sort the given array, that it is an in-place sorting technique.
[8,1,5,14,4,15,12,6,2,11,10,7,9]
5
6
Tower of Hanoi is a m
athematical puzzle where we have 3
rods and N
disks. The
objective of the puzzle is to move all disks from source rod to d
estination rod
using a t hird rod (say auxiliary). The rules are :
● Only one disk can be moved at a time.
● A disk can be moved only if it is on the top of a rod.
● No disk can be placed on the top of a smaller disk.
Print the steps required to move N
disks from source rod to destination rod.
Source Rod is named as ' A', the destination rod as ' B', and the auxiliary rod as ' C'.
Let's see how to solve the problem recursively. We'll start with a really easy case
N=1. We just need to move one disk from source to destination.
● You can always move disk 1 from peg A to peg B because you know that any
disks below it must be larger.
● There's nothing special about pegs A and B. You can move disk 1 from peg B
to peg C if you like, or from peg C to peg A
, or from any peg to any peg.
● Solving the Towers of Hanoi problem with one disk is trivial as it requires
moving only the one disk one time.
Now consider the case N=2. Here's what it looks like at the start:
7
First, move disk 1 from peg A to peg C
:
8
Now let us solve this problem for 3 disks. You need to expose the bottom disk (disk
3) so that you could move it from peg A to peg B. To expose disk 3, you needed to
move disks 1 and 2 from peg A to the spare peg, which is peg C
:
Wait a minute—it looks like two disks moved in one step, violating the first rule. But
they did not move in one step. You agreed that you can move disks 1 and 2 from
any peg to any peg, using three steps. The situation above represents what you
have after three steps. (Move disk 1 from peg A to peg B; move d
isk 2 from peg A
to peg C
; move d
isk 1 from peg B
to peg C. )
9
Now, to finish up, you need to recursively solve the subproblem of moving disks
1 through n-1, from peg C
to peg B
. Again, you agreed that you can do so in three
steps. (Move d
isk 1 from peg C
to peg A
; move disk 2 from peg C to peg B; move
disk 1 from peg A
to peg B.) And you're done:
At this point, you might have picked up the pattern. The algorithm can be
summarised as:
10
Java Code
// driver code
public static void main(String[] args) {
int n = 4;
TowerOfHanoi(n,'A','B','C'); // A, B, C are the name of rods
}
11
12