Python Programming: Recursion, Recursive Function Searching, Sorting and Merging
Python Programming: Recursion, Recursive Function Searching, Sorting and Merging
Example:
A physical world example would be to place two parallel mirrors facing
each other. Any object in between them would be reflected recursively.
NESTED DOLLS
FRACTALS
Recursive Function in python
• a function can call other functions.
• It is even possible for the function to call itself.
• These types of functions which call itself till the certain condition
is not met are termed as recursive functions.
Factorial through recursion
Termination Condition
• A recursive function needs a condition to terminate.
• Moving towards a condition where the problem can be solved without
further recursion, a recursive function will terminate, minimizing the
problem into the smaller sub-steps.
• A recursion can end up in an infinite loop if the condition of termination is
not met in the calls.
Factorial conditions:
•factorial of n = n * (n-1) as long as n is greater than 1.
•1 if n = 0
Advantages of Recursion
▪ Recursive functions make the code look clean and elegant.
▪ A complex task can be broken down into simpler sub-problems using recursion.
▪ Sequence generation is easier with recursion than using some nested iteration.
Disadvantage
▪ Recursive calls are expensive (inefficient) as they take up a lot of memory and
time.
▪ Recursive functions are hard to debug.
Fibonacci Sequence Using Recursion
• A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....
• first two terms are 0 and 1.
• All other terms are obtained by adding preceding two terms.
• nth term is the sum of (n-1)th and (n-2)th term.
• It is an example of binary recursion
Fibonacci sequence program
What is Sorting?
▪ Sorting means putting items in a particular order.
▪ That particular order is determined by the comparison property of the
elements.
▪ In the case of integers, we say smaller number comes first and bigger
number appear later.
▪ Arranging items in particular order improves the searching of the element.
▪ Hence sorting has heavy usage in computer science.
Sorting method/algorithm
▪ Bubble sort
▪ Selection sort
▪ Insertion sort
▪ Merge sort
Bubble sort
• Bubble sort, sometimes referred to as sinking sort, is a simple
sorting algorithm
• it repeatedly steps through the list to be sorted, compares each
pair of adjacent items and swaps them if they are in the wrong
order.
• The pass through the list is repeated until no swaps are
needed, which indicates that the list is sorted.
Example: Bubble sort
Take an array of numbers " 20 9 6 3 1", and sort the array from lowest number to greatest
number.
Bubble sort: Program
Selection sort
• algorithm starts by finding smallest number in the list and
exchanging it with the first number.
• next smallest number is found and exchanged with the second
number, and so on.
• Use of the selection sort to order the five elements of the array
is described below. Smallest element of each pass is written in
blue background.
Selection sort : example
Selection sort: Python program
Insertion Sort
• It is a sorting algorithm that works similar to way you sort playing cards in
your hands.
• array is virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed at the correct position in the sorted
part.
Insertion sort
Insertion sort : python program
Insertion sort: Example
Merge two sorted list
Merging two sequence means combining two separate sequence into one
single sequence.
Method 1: Merging
• check for the smaller of two element on the current index and
increment the index of the list whose no. is encountered.
• When either of list gets exhausted, the other list is appended to the
end of merged list.
Method 2: Using sorted() method
Merge sort
• Merge sort is one of the most efficient sorting algorithms.
• It works on the principle of Divide and Conquer.
• Merge sort repeatedly breaks down a list into several sublists until each
sublist consists of a single element and merging those sublists in a manner
that results into a sorted list.
Divide step:
Merge step
Tower of Hanoi Problem
It is a mathematical puzzle where we have three rods and n disks. The
objective of puzzle is to move the entire stack to another rod. These rings
are of different sizes and stacked upon in an ascending order, i.e. the
smaller one sits over the larger one.
Rules:
1.Only one disk can be moved at a time.
2.Each move consists of taking the upper disk from one of the stacks
and placing it on top of another stack i.e. a disk can only be moved if
it is the uppermost disk on a stack.
3.No disk may be placed on top of a smaller disk.
Tower of Hanoi Problem
• Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps.
• This presentation shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps.
Python program: TOH