Recursion and Merge Sort
Recursion and Merge Sort
AND TECHNOLOGY
UNIVERSITY
COURSE CODE CIT-121
SUBMITTED TO:
Prof. Dr. M. A. Masud
Department of Computer and Information Technology
Faculty of Computer Science and Engineering
SUBMITTED BY:
Md. Sharafat Karim
ID: 2102024,
Registration No: 10151
Faculty of Computer Science and Engineering
In this way we can call the same function from itself to find the result of the sequence. With
recursion we can write graph and binary tree algorithms more efficiently. It is also vastly
used in dynamic programming.
At first we will work with the bottom numbers. Here we will sort 8 and 2 into a new list.
Thus it will become 2, 8. Later we will compare it with 4. So we need to functions. One
function is to split the array into two smaller parts. And then we will use an another
function to merge them back altogether. Here’s the algorithm for merging two sorted array,
On the above example if we run the merge sort after finding the sorted sub arrays we are
backtracking to the parent node. Then we are using the merge again and again to achieve
result. So in the worst case scenario we have to iterate through we have to check through
all of the values.
Time complexity
Time complexity of merge sort is n log(n). Here we are splitting the array into two parts like
binary search. So the number of comparison is O(n log(n)).
#include <bits/stdc++.h>
using namespace std;
int main() {
int ar[] = {8, 2, 4, 6, 9, 7, 10, 1, 5, 3};
int* sorted = mergeSort(ar, sizeof(ar)/ sizeof(int));
print(sorted, sizeof(ar)/ sizeof(int));
}
Here the ar is a one dimensional array data structure. We use the merge sort algorithm on
this array. We are using pointers to pass as a reference and then using an another array to
copy it’s contents. And after sorting we are using malloc for dynamic programming to
create a permanent memory space and returning it’s location which is the final result.
Induction
Induction is the process of proving theorems of sequence summation of iterative patterns.
Here’s an example,
let’s consider a equation 1 + 2 + 22 + ⋯ + 2n = 2n+1 − 1.
Solution:
Let P(n) be the proposition that 1 + 2 + 22 + ⋯ + 2n = 2n+1 − 1 for the integer n.
P(0) is true because 20 = 1 = 21 − 1.
This completes the basis step.
So these two terms are same. So we can say that our induction process is working perfectly.