B036-Expt No.2 Aoa
B036-Expt No.2 Aoa
B036-Expt No.2 Aoa
Experiment No.02
A.1 Aim:
Write a program to implement Merge sort / Binary Search using Divide and
Conquer Approach and analyze its complexity.
A.2 Prerequisite: -
A.3 Outcome:
After successful completion of this experiment students will be able to analyze
the time complexity of various classic problems.
A.4 Theory:
Merge sort is based on the divide-and-conquer paradigm. Its worst-case running
time has a lower order of growth than insertion sort. Since we are dealing with
subproblems, we state each subproblem as sorting a subarray A[p .. r]. Initially, p =
1 and r = n, but these values change as we recurs through subproblems.
1. Divide Step
If a given array A has zero or one element, simply return; it is already sorted.
Otherwise, split A[p .. r] into two subarrays A[p .. q] and A[q + 1 .. r], each
containing about half of the elements of A[p .. r]. That is, q is the halfway point
of A[p .. r].
2. Conquer Step
Conquer by recursively sorting the two subarrays A[p .. q] and A[q + 1 .. r].
3. Combine Step
MERGE (A, p, q, r)
n1 ← q − p + 1
n2 ← r − q
Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]
FOR i ← 1 TO n1
DO L[i] ← A[p + i − 1]
FOR j ← 1 TO n2
DO R[j] ← A[q + j ]
L[n1 + 1] ← ∞
R[n2 + 1] ← ∞
i←1
j←1
FOR k ← p TO r
DO IF L[i ] ≤ R[ j]
THEN A[k] ← L[i]
i←i+1
ELSE A[k] ← R[j]
j←j+1
Time Complexity:
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the concerned lab
in charge faculties at the end of the practical in case the there is no Black board access
available)
//MERGE SORT
#include<stdio.h>
#include<conio.h>
int n=0,b[20],c=0;
int i,j,k;
c++;
i=l;
c++;
j=mid+1;
c++;
k=l;
c++;
if(a[i]<a[j])
b[k++]=a[i++];
c++;
else
b[k++]=a[j++];
c++;
while(i<=mid)
b[k++]=a[i++];
c++;
while(j<=r)
b[k++]=a[j++];
c++;
a[i]=b[i];
c++;
int mid;
c++;
if(l<r)
mid=(l+r)/2;
c++;
mergesort(a,l,mid);
c++;
mergesort(a,mid+1,r);
c++;
merge(a,l,mid,r);
c++;
void main()
int i;
int a[20];
clrscr();
scanf("%d",&n);
scanf("%d",&a[i]);
mergesort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
getch();
//BINARY SEARCH
#include<stdio.h>
#include<conio.h>
int high,low,mid;
low = 0;
if (A[mid] == key) {
return mid;
high = mid - 1;
else {
low = mid + 1;
return -1;
int main() {
int a[10],key,position,size,i;
clrscr();
printf("\tBINARY SEARCH\n\n");
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
scanf("%d",&key);
if (position == -1){
printf("Not found");
return 0;
}
printf("The position of the element is %d", position+1);
getch();
return 0;
MERGE SORT:
BINARY SEARCH :
B.3 Observations and learning:
Merge sort and binary search are classic algorithms based on the divide
and conquer approach. Merge sort divides an array into halves, sorts them,
and merges them, with a time complexity of O(n log n). Binary search
divides a sorted array in half to efficiently find a target element, with a time
complexity of O(log n). Learning and implementing these algorithms
provide insights into recursion, sorting, searching, and algorithmic
analysis,fostering a deeper understanding of efficient problem-solving
techniques.
B.4 Conclusion: