PYTHON UNIT-5 - (Notes-1)
PYTHON UNIT-5 - (Notes-1)
(KNC402)
Lecture Notes
(Unit-4)(Notes-1)(Iterators &
Recursion, Searching, Sorting and
Merging)
PYTHON PROGRAMMING UNIT-4
# using recursion
def Factorial(n):
# declare a base case (a limiting criteria)
if n == 1:
return 1
# continue with general case
else:
return n * Factorial(n-1)
print(Factorial(10))
Output:
3628800
A recursive function, named Factorial(), is defined with the limiting criteria of n=1. It
first attempts to find the factorial of 10. Factorial(10) is broken down into 10 *
Factorial(9). Further, Factorial(9) is broken down into 9 * Factorial(8), and so on. When
Factorial(1) is called, it stops the recursion. Factorial(10) awaits for the value of
Factorial(9). Factorial(9) awaits for the value of Factorial(8), and so on. Thus once the
limiting criteria (here, n=1) is reached, it starts returning the values.
PYTHON PROGRAMMING UNIT-4
Recursive Fibonacci:
1. Fibonacci series is a series of numbers formed by the addition of the preceding two
numbers in the series.
2. It is simply the series of numbers which starts from 0 and 1 and then continued by
the addition of the preceding two numbers.
3. Example of Fibonacci series: 0, 1, 1, 2, 3, 5.
#Output :
Enter the term : 5
Fibonacci sequence:
01123
PYTHON PROGRAMMING UNIT-4
Output :
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
PYTHON PROGRAMMING UNIT-4
Searching Algorithm
When the data is stored in a machine and after a certain amount of time the same data is
to be retrieved by the user, the computer uses the searching algorithm to find the data.
The system jumps into its memory, processes the search of data using the searching
algorithm technique, and returns the data that the user requires. Therefore, the searching
algorithm is the set of procedures used to locate the specific data from the collection of
data.
Two of the searching algorithms are linear search and binary search.
Linear Search
Linear search is also known as a sequential searching algorithm to find the element
within the collection of data. The algorithm begins from the first element of the list,
starts checking every element until the expected element is found. If the element is not
found in the list, the algorithm traverses the whole list and return “element not found”.
Therefore, it is just a simple searching algorithm.
Example:
Consider the below array of elements. Now we have to find element a = 1 in the array
given below.
We will start with the first element of the array, compare the first element with the
element to be found. If the match is not found, we will jump to the next element of the
array and compare it with the element to be searched i.e „a‟.
If the element is found, we will return the index of that element else, we will return
'element not found'.
PYTHON PROGRAMMING UNIT-4
Output:
Element found at index: 3
Binary Search
Binary search is used with a similar concept, i.e to find the element from the list of
elements. Binary search algorithms are fast and effective in comparison to linear search
algorithms. The most important thing to note about binary search is that it works only on
sorted lists of elements. If the list is not sorted, then the algorithm first sorts the elements
using the sorting algorithm and then runs the binary search function to find the desired
output.
Example
Let the elements of array are -
Now, the element to search is found. So algorithm will return the index of the element
matched.
#Program for Binary Search (Iterative Method)
def binarySearch (arr, k, low, high):
while low <= high:
mid = (low + high)//2
if arr[mid] == k:
return mid
elif arr[mid] < k:
low = mid + 1
else:
high = mid - 1
return -1
arr = [1, 3, 5, 7, 9]
k=5
result = binarySearch(arr, k, 0, len(arr)-1)
if result != -1:
print("Element is present at index " + str(result))
else:
print("Not found")
Output:
Element is present at index 2
PYTHON PROGRAMMING UNIT-4
Output:
Element is present at index 2
Selection sort is a comparison sorting algorithm that is used to sort a random list of items in
ascending order. The comparison does not require a lot of extra space. It only requires one
extra memory space for the temporal variable.
This is known as in-place sorting. The selection sort has a time complexity of O(n2) where n
is the total number of items in the list. The time complexity measures the number of
iterations required to sort the list. The list is divided into two partitions: The first list contains
sorted items, while the second list contains unsorted items.
The unsorted list is then scanned for the minimum value, which is then placed in the sorted
list. This process is repeated until all the values have been compared and sorted.
3. After each iteration, minimum is placed in the front of the unsorted list.
Output:
[-9, -2, 0, 11, 45]
PYTHON PROGRAMMING UNIT-4
Merge step
PYTHON PROGRAMMING UNIT-4
l=[1,6,4,2,4,7,8,3,5,10]
merge_sort(l)
print(l)
Output:
[1, 2, 3, 4, 4, 5, 6, 7, 8, 10]