Aoa Lab Manual
Aoa Lab Manual
Aoa Lab Manual
LAB MANUAL
Analysis of Algorithm
Class SE Sem IV
Prepared By
Swati Shelar
2024-2025
List of Experiment
Description:
First pass:
For the first position in the sorted array, the whole array is traversed
from index 0 to 4 sequentially. The first position where 64 is stored
presently, after traversing whole array it is clear that 11 is the
lowest value.
Thus, replace 64 with 11. After one iteration 11, which happens to
be the least value in the array, tends to appear in the first position
of the sorted list.
Second Pass:
For the second position, where 25 is present, again traverse the rest
of the array in a sequential manner.
After traversing, we found that 12 is the second lowest value in the
array and it should appear at the second place in the array, thus
swap these values.
Third Pass:
Now, for third place, where 25 is present again traverse the rest of
the array and find the third least value present in the array.
While traversing, 22 came out to be the third least value and it
should appear at the third place in the array, thus swap 22 with
element present at third position.
Fourth pass:
Similarly, for fourth position traverse the rest of the array and find
the fourth least element in the array
As 25 is the 4th lowest value hence, it will place at the fourth
position.
Fifth Pass:
At last the largest value present in the array automatically get
placed at the last position in the array.
The resulted array is the sorted array.
Time Complexity:
The time complexity of Selection Sort is O(N2) as there are two nested
loops:
One loop to select an element of Array one by one = O(N)
Another loop to compare that element with every other Array
element = O(N)
Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N )
2
Auxiliary Space: O(1) as the only extra memory used is for temporary
variables while swapping two values in Array. The selection sort never
makes more than O(N) swaps and can be useful when memory writing
is costly.
Advantages of Selection Sort Algorithm
PROGRAM :
#include<stdio.h>
#include <conio.h>
int main()
{
int a[100], number, i, j, temp;
printf("\n Please Enter the total Number of Elements : ");
scanf("%d", &number);
printf("\n Please Enter the Array Elements : ");
for(i = 0; i < number; i++)
scanf("%d", &a[i]);
Description:
Algorithm :
The simple steps of achieving the insertion sort are listed as follows -
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current
element, then move to the next element. Else, shift greater elements in
the array towards the right.
Time Complexity :
Case Time Complexity
Program :
#include <stdio.h>
int main()
{
int a[100], number, i, j, temp;
Description:
The “Merge Sort” uses a recursive algorithm to achieve its results. The
divide-and-conquer algorithm breaks down a big problem into smaller,
more manageable pieces that look similar to the initial problem. It then
solves these subproblems recursively and puts their solutions together
to solve the original problem.
Step 1: Start
Step 2: Declare an array and left, right, mid variable
Step 3: Perform merge function.
mergesort(array,left,right)
mergesort (array, left, right)
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)
Step 4: Stop
#include<stdio.h>
#include<conio.h>
int a[50];
void merge(int,int,int);
int mid;
if(low<high)
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
if(a[h]<=a[j])
b[i]=a[h];
h++;
else
b[i]=a[j];
j++;
i++;
if(h>mid)
for(k=j;k<=high;k++)
b[i]=a[k];
i++;
}
}
else
for(k=h;k<=mid;k++)
b[i]=a[k];
i++;
for(k=low;k<=high;k++) a[k]=b[k];
int main()
int num,i;
printf("\t\t\tMERGE SORT\n");
scanf("%d",&num);
for(i=1;i<=num;i++)
scanf("%d",&a[i]);
}
merge_sort(1,num);
for(i=1;i<=num;i++) printf("\t%d",a[i]);
getch();
OUTPUT :
Theory :
Algorithm:
Step 4: Divide the array list into two halves the lower array list and
upper array list using the merge sort function.
Time complexities:
Quick sort has an average time of O(n log n) on n elements. Its worst
case time is O(n²)
Program:
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
return 0;
}
OUTPUT:
Theory:
Binary search follows the divide and conquer approach in which the list
is divided into two halves, and the item is compared with the middle
element of the list. If the match is found then, the location of the
middle element is returned. Otherwise, we search into either of the
halves depending upon the result produced through the match.
Algorithm :
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
OUTPUT:
Theory:
Algorithm
1. There will be two loops: the outer loop will range from i=0 to i≤n-m,
and the inner loop will range from j=0 to j<m, where ‘m’ is the
length of the input pattern and n is the length of the text string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int status;
gets(st);
gets(pat);
if (status == -1)
else
return 0;
m = strlen(pat);
temp++;
if (st[i + j] == pat[j])
count++;
if (count == m)
return temp;
count = 0;
return -1;
}
OUTPUT
Theory:
Knuth-Morris and Pratt introduce a linear time algorithm for the string
matching problem. A matching time of O (n) is achieved by avoiding
comparison with an element of 'S' that have previously been involved in
comparison with some element of the pattern 'p' to be matched. i.e.,
backtracking on the string 'S' never occurs
2. The KMP Matcher: With string 'S,' pattern 'p' and prefix function 'Π'
as inputs, find the occurrence of 'p' in 'S' and returns the number of
shifts of 'p' after which occurrences are found.
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int M = strlen(pat);
int N = strlen(txt);
// create lps[] that will hold the longest prefix suffix values for pattern
computeLPSArray(pat, M, lps);
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
if (j == M) {
j = lps[j - 1];
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
int i;
i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
if (len != 0) {
} else // if (len == 0)
lps[i] = 0;
i++;
int main() {
return 0;
OUTPUT:
Theory:
all queens are safe. The queen moves in 8 directions and can directly
attack in these 8 directions only.
Before starting with the actual N queen problem, let's shorten it down
to 4 - Queen problem, and then we will generalize it for N.
N Queen Problem Algorithm :
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[30],count=0;
{
int i;
for (i=1;i<pos;i++)
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
return 1;
void print_sol(int n)
int i,j;
count++;
printf("\n\nSolution #%d:\n",count);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if(a[i]==j)
printf("Q\t");
else printf("*\t");
printf("\n");
}
void queen(int n)
int k=1;
a[k]=0;
while(k!=0)
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n)
if(k==n)
print_sol(n);
else
k++;
a[k]=0;
else k--;
}
void main()
int i,n;
clrscr();
scanf("%d",&n);
queen(n);
printf("\nTotal solutions=%d",count);
getch();
}
OUTPUT
Theory:
has the minimum sum of weights among all the trees that can be
formed from the graph
It falls under a class of algorithms called greedy algorithms that find the
local optimum in the hopes of finding a global optimum.
We start from one vertex and keep adding edges with the lowest
weight until we reach our goal.
2. Find all the edges that connect the tree to new vertices, find the
minimum and add it to the tree
PROGRAM :
#include<stdio.h>
#include<conio.h>
int n, cost[10][10];
void prim() {
temp = cost[0][0];
temp = cost[i][j];
startVertex = i;
endVertex = j;
tree[0][1] = endVertex;
tree[0][2] = temp;
minimumCost = temp;
nr[i] = startVertex;
else
nr[i] = endVertex;
nr[startVertex] = 100;
nr[endVertex] = 100;
temp = 99;
temp = cost[j][nr[j]];
k = j;
}
}
tree[i][0] = k;
tree[i][1] = nr[k];
tree[i][2] = cost[k][nr[k]];
nr[k] = 100;
nr[j] = k;
temp = 99;
printf("%d", tree[i][j]);
printf("\n");
}
printf("\nMin cost : %d", minimumCost);
void main() {
int i, j;
clrscr();
scanf("%d", &n);
scanf("%d", &cost[i][j]);
printf("%d\t", cost[i][j]);
printf("\n");
prim();
getch();
}
OUTPUT :
Theory:
Applications of LCS:
This algorithm has a time complexity of O(mn), where m and n are the
lengths of the two strings. It has a space complexity of O(mn) as well
since it requires a two-dimensional array to store the intermediate
values.
Other algorithms for finding LCS include divide and conquer and suffix
trees. These algorithms have different time and space complexity
characteristics, and may be more suitable for different types of
problems.
PROGRAM :
#include<stdio.h>
#include<string.h>
int i,j,m,n,c[20][20];
char x[20],y[20],b[20][20];
void print(int i,int j)
if(i==0 || j==0)
return;
if(b[i][j]=='c')
print(i-1,j-1);
printf("%c",x[i-1]);
else if(b[i][j]=='u')
print(i-1,j);
else
print(i,j-1);
void lcs()
m=strlen(x);
n=strlen(y);
for(i=0;i<=m;i++)
c[i][0]=0;
for(i=0;i<=n;i++)
c[0][i]=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if(x[i-1]==y[j-1])
c[i][j]=c[i-1][j-1]+1;
b[i][j]='c';
else if(c[i-1][j]>=c[i][j-1])
c[i][j]=c[i-1][j];
b[i][j]='u';
else
c[i][j]=c[i][j-1];
b[i][j]='l';
}
}
int main()
scanf("%s",x);
scanf("%s",y);
lcs();
print(m,n);
return 0;
}
Output :