Professional Documents
Culture Documents
LAB MANUAL
Dynamic
12 Implement LCS problem.
Programming
1|Page
PRACTICAL 1 :
AIM: - Implementation and Time analysis of
sorting algorithms. Bubble sort,
Selection sort, Insertion sort, Merge sort and
Quicksort
180063131001
2|Page
Algorithm(pseudo code) :-
180063131001
3|Page
Program : =
#include<stdio.h>
#include<conio.h>
void bubble(int a[10],int n);
void main()
{
int n,i;
int a[10];
clrscr();
printf("enter the value of elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("the array element is..");
scanf("%d",&a[i]);
}
bubble(a,n);
getch();
}
void bubble(int a[10],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<i;j++)
{
if(a[j]>a[j+1])
180063131001
4|Page
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("the sorted array is....");
for(i=0;i<n;i++)
{
printf("\n");
printf("%d",a[i]);
}
}
Output:
180063131001
6|Page
Time analysis :-
Best case = O(n^2)
Avg case = O(n^2)
Worst cae = O(n^2)
180063131001
7|Page
Program :-
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;
180063131001
8|Page
return 0;
180063131001
9|Page
out put:-
180063131001
10 | P a g e
Time analysis:-
Algorithm:
i←1
while i < length(A)
180063131001
11 | P a g e
j←i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j←j-1
end while
i←i+1
end while
program:-
#include<stdio.h>
#include<conio.h>
void insertion(int a[10],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
temp = a[i];
j = i-1;
while((j>=0)&&(a[j]>temp))
180063131001
12 | P a g e
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\n the sorted array is...");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
}
void main()
{
int a[10],n,i;
clrscr();
printf("enter the value of n: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
180063131001
13 | P a g e
scanf("%d",&a[i]);
}
insertion(a,n);
printf("\n successfull!!");
getch();
}
Output:-
180063131001
14 | P a g e
Theory:-
Quick sort is a highly efficient sorting algorithm
and is based on partitioning of array of data into
smaller arrays. A large array is partitioned into
two arrays one of which holds values smaller
than the specified value, say pivot, based on
which the partition is made and another array
holds values greater than the pivot value.
Time complexity:-
Best case:- O(N)
Avg. case:- O(NlogN)
Worst case:- O(N)
180063131001
15 | P a g e
Algorithm:-
quicksort(A, lo, hi)
if lo < hi then
p := partition(A, lo, hi)
quicksort(A, lo, p)
quicksort(A, p + 1, hi)
Program:-
#include<stdio.h>
#include<conio.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
180063131001
17 | P a g e
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size)
{
180063131001
18 | P a g e
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[],m,n,i;
printf(“enter the size of array that is to be
sorted: “);
scanf(“%d”,&m);
printf(“enter the elements of the array one by
one”);
for(i=0;i<m;i++)
{
Scanf(“%d”,&a[i]);
}
quickSort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
180063131001
19 | P a g e
return 0;
180063131001
20 | P a g e
Time analysis:-
Best case:- O(NlogN)
Avg. case:- O(NlogN)
Worst case:- O(NlogN)
180063131001
21 | P a g e
Algorithm:-
MergeSort(arr[], l, r)
If r > l
1. Find the middle index of the array to
divide it in two halves:
m = (l+r)/2
2. Call MergeSort for first half:
mergeSort(array, l, m)
3. Call mergeSort for second half:
mergeSort(array, m+1, r)
4. Recursively, merge the two halves in a
sorted manner, so that only one sorted array
is left:
merge(array, l, m, r)
180063131001
22 | P a g e
Program:-
#include<stdio.h>
#include<conio.h>
void merge(int a[10], int low,int high)
{
int mid, a[10];
void combine(int a[10], int low, int mid, int
high);
if(low<high)
{
mid = (low+high)/2;
merge(a,low,mid);
merge(a,mid+1,high);
combine(a,low,mid,high);
}
}
void combine(int a[10], int low, int mid, int high)
{
int i,j,k;
180063131001
23 | P a g e
int temp[10];
k=low;
i=high;
j=mid+1;
while(i<=mid&&j<=high)
{
if(a[i]<=a[j])
{
temp[k]=a[i];
i++;
k++;
}
else
{
temp[k]=a[j];
j++;
k++;
}
}
while(i<mid)
180063131001
24 | P a g e
{
temp[k]=a[i];
i++;
k++;
}
while(j<=high)
{
temp[k]=a[j];
j++;
k++;
}
for(k=low;k<=high;k++)
{
a[k]=temp[k];
}
}
void main()
{
int a[10],i,n;
printf("enter the element you want to sort: ");
180063131001
25 | P a g e
scanf("%d",&n);
printf("the array elements are...");
for(i=0;i<n;i++)
{
printf("\n");
scanf("%d",&a[i]);
}
merge(a,low,high);
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
}
180063131001
26 | P a g e
Maximu
Notes Description Marks Evaluation m Actual
Marks Marks
0 1 2
A Laboratory Attendance Absent Late Present 2
Signature
180063131001
27 | P a g e
PRACTICAL 2 :
Aim: Implementation and Time analysis of linear and
binary search algorithm.
Algorithm:-
Linear Search ( Array A, Value x)
Step 1: Set i to 1
180063131001
28 | P a g e
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[10],key,x;
printf("enter the number the of element in
array: ");
scanf("%d",&n);
printf("enter bumber");
180063131001
29 | P a g e
for(i=0;i<n;i++)
{
prntf("\n");
scanf("%d",&a[i]);
}
printf("enter the number to search: ");
scanf("%d",&key");
for(i=0;i<n;i++)
{
if(a[i]=key)
{
printf("number is prsent in array");
x=a[i];
printf("%d",x);
}
else
{
i=i+1;
}
180063131001
30 | P a g e
}
getch();
Time complexity:-
Algorithm:-
Binarysearch(a[0…n-1],key)
Low=0;
High=n-1;
While(low<high) do
{
M=(low+high)/2;
If(key=a[m]) then
Return m
Else if(key<a[m]) then
High = m-1
Else
Low=m+1
180063131001
32 | P a g e
}
Return -1
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],key,i,flag,low,high,n;
int binarysearch(int a[10],int key,int low,int
high);
clrscr();
printf("enter the number of elements: ");
scanf("%d",&n);
printf("enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
180063131001
33 | P a g e
180063131001
34 | P a g e
binarysearch(a,key,low,mid-1);
}
else
{
binarysearch(a,key,mid+1,high);
}
return 0;
}
Maximu
Notes Description Marks Evaluation m Actual
180063131001
35 | P a g e
Marks Marks
0 1 2
A Laboratory Attendance Absent Late Present 2
PRACTICAL 3:
180063131001
36 | P a g e
Theory:-
• Heap sort is the sorting method discovered
by J.W.J. Williams.
• It is one type of complete binary tree.
• It satisfy all the properties of the complete
binary tree.
• The main purpose behind the creation of the
heap sort is to reduce the time complexity
up to O(logn) from Θ(n )2
Heap properties:
• There are two kind of binary heaps: max‐
heaps and min‐heaps.
180063131001
37 | P a g e
180063131001
38 | P a g e
if 2 ∗ i ≤ heap-size[A]
return 2 ∗ i
else return NULL
r ← Right(i)
if l ≤ heap-size[A] and A[l] > A[i]
largest ← l
else largest ← i
if r ≤ heap-size[A] and A[r] < A[largest]
largest ← r
if largest 6= i
180063131001
39 | P a g e
Max-Heapify(A, i)
Program:-
180063131001
40 | P a g e
#include <stdio.h>
#include<conio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
180063131001
41 | P a g e
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n)
{
int i;
for (i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
180063131001
42 | P a g e
printf("\n");
}
void main()
{
int arr[10],n,i;
clrscr();
printf("enter the number of elements:");
scanf("%d",&n);
printf("enter the elements to sort:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
heapSort(arr, n);
180063131001
43 | P a g e
Maximu
Notes Description Marks Evaluation m Actual
Marks Marks
0 1 2
A Laboratory Attendance Absent Late Present 2
180063131001
44 | P a g e
PRACTICAL 4:
THEORY
Compute the factorial function F(n) = n! for an
arbitrary nonnegative integer n.
Since
Algorithm:-
ALGORITHM factorial_iterative (n)
ans ← 1
if n=0 return 1
180063131001
45 | P a g e
else
for j←1 to n do
ans *= j
return ans
ALGORITHM Factorial_recursive(n)
if n = 0 return 1
else return Factorial_recursive (n − 1) * n
time analysis:-
C (n) = O (n!)
Program :-
#include <stdio.h>
int factorial(int n)
{
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
180063131001
46 | P a g e
return res;
}
void main()
{
int num;
clrscr();
printf("enter the number:");
scanf("%d",&num);
printf("Factorial of %d is %d", num,
factorial(num));
getch();
}
OUTPUT:
180063131001
47 | P a g e
Maximu
Notes Description Marks Evaluation m Actual
Marks Marks
0 1 2
A Laboratory Attendance Absent Late Present 2
180063131001