Analysis of Search and Sorting Algorithms
Analysis of Search and Sorting Algorithms
#include<stdio.h>
#include<conio.h>
void lin_search(int b[50],int n);
int bin_search(int b[50],int low,int high);
int item,cost = 0;
void main( )
{
int a[50],i,n,loc;
clrscr( );
if(loc==0)
printf("Unsuccessful serach.%d not found.\n",item);
else
{
printf("Successful search\n");
printf("%d found at position %d.\n",item,loc);
}
printf("The cost of running binary search is %d\n",cost);
getch( );
}
void lin_search(int b[50],int n)
{
int i,flag = 1,cost = 0 ;
for(i=0;i<n;i++)
{
cost++;
if(b[i]==item)
{
printf("Successful search\n");
printf("%d found at position %d.\n",item,i+1);
flag = 0;
break;
}
}
if(flag)
printf("Unsuccessful serach.%d not found.\n",item);
printf("The cost of running linear search is %d\n",cost);
}
Study and analyse the average case, worst case and best case for sorting
[Link] discuss various factors on which running time of
algorithm depends :
1) Insertion sort.
2) Merge sort.
3) Quick sort.
#include<stdio.h>
#include<conio.h>
void insertion_sort(int a[],int n);
void merge_sort(int a[],int p,int r);
void merge(int a[],int p,int q,int r);
int partition(int a[],int p,int r);
void quick_sort(int a[],int p,int r);
int cost = 0;
void main( )
{
int n,a[50],b[50],c[50],i,j;
clrscr( );
printf("Enter the number of elements to be sorted :");
scanf("%d",&n);
printf("\nEnter the elements :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
b[i] = a[i];
c[i] = a[i];
}
for(i=1;i<=n;i++)
printf("%d ",c[i]);
printf("\nThe cost of running quick sort is %d",cost);
getch();
for(j=2;j<=n;j++)
{
cost++;
key = a[j];
i = j-1;
while(i>0 && a[i]>key)
{
cost++;
a[i+1] = a[i];
i = i-1;
}
a[i+1] = key;
}
for(i=1;i<=n;i++)
printf("%d ",a[i]);
printf("\nThe cost of running insertion sort is %d",cost);
}
OUTPUT
PRIM’S ALGORITHM
#include<stdio.h>
#include<conio.h>
#define size 20
typedef enum
{
true = 1,
false = 0
}boolean;
for(i=0;i<n;i++)
if(a[i]&&(!m||m>a[i]))
{
m = a[i];
*j = i;
}
return m;
}
void main( )
{
int a[size][size],c[size],i,j,k,l,m,n,nodused,cost,minm;
boolean f1,isof;
clrscr( );
for(i=0;i<n;i++)
{
a[i][i] = 0;
for(j=i+1;j<n;j++)
{
if(a[i][j]==0)
a[i][j] = a[j][i];
else
if(a[j][i]<a[i][j]&&a[j][i]!=0)
a[i][j] = a[j][i];
a[j][i] = a[i][j];
}
}
printf("The nodes to be connected in spanning tree are :\n");
f1 = false;
c[0]=1;
cost = 0;
nodused = 1;
while(!f1)
{
minm = 0;
isof = false;
for(i=0;i<n;i++)
if(c[i]!=0)
{
m = min(a[i],n,&j);
if(j>-1)
if(c[j]!=0)
{
a[i][j] = 0;
a[j][i] = 0;
isof = true;
}
else
if(minm==0||(m<minm&&m!=0))
{
minm = m;
k = j;
l = i;
}
}
if(minm!=0)
{
printf("(%d,%d);",l+1,k+1);
cost = cost +a[l][k];
a[l][k] = 0;
a[k][l] = 0;
c[k] = 1;
nodused = nodused+1;
}
else if(!isof)
{
printf("Isolated nodes found!\n");
f1 = true;
}
if(nodused>=n)
f1 = true;
}
printf("\n");
printf("The cost of tree is :%d",cost);
getch( );
}
OUTPUT
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#define max(a,b)((a)>(b)?(a):(b))
struct ll_node{
struct ll_node *next;
int src,dest;
int weight;
};
int *parent;
char err_iso[ ] = "some nodes in the tree are isolated\n";
void error(char *msg)
{
puts(msg);
exit(1);
}
void put_sorted(struct ll_node *phead,struct ll_node *node)
{
struct ll_node *current,*prev;
prev = phead;
current = phead->next;
while(current&¤t->weight<node->weight)
{
prev = current;
current = current->next;
}
node->next = current;
prev->next = node;
}
while(sscanf(inps,"%d%d%d",&i,&j,&weight)==3)
{
parent = (int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
parent[i] = i;
current = phead->next;
while(current&¤t->src==current->dest)
current = current->next;
if(!current)
error(err_iso);
first = current;
parent[first->dest] = first->src;
nedges = 1;
current = first->next;
first->next = NULL;
while(current||nedges<n-1)
{
temp = current->next;
root_src = root(parent,current->src);
root_dest = root(parent,current->dest);
if(root_dest!=root_src)
{
current->next = first;
first = current;
parent[root_dest] = root_src;
nedges++;
}
else
free(current);
current = temp;
}
phead->next = first;
return nedges;
}
void main( )
{
int n;
struct ll_node head;
struct ll_node *current;
clrscr();
[Link] = NULL;
n = input(&head);
if(min_span(&head,n)!=n-1)
error(err_iso);
current = [Link];
printf("The minimum spanning tree has the following edges:\n");
while(current)
{
printf("%d-%d\n",current->src+1,current->dest+1);
current = current->next;
}
getch( );
}
OUTPUT
#include<stdio.h>
int * counting_sort(int A[ ],int B[ ],int k,int n)
{
int i,j,C[20];
for(i=0;i<=k;i++)
C[i] = 0;
for(j=1;j<=n;j++)
C[A[j]] = C[A[j]]+1;
for(i=1;i<=k;i++)
C[i] = C[i]+C[i-1];
for(j=n;j>=1;j--)
{
B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]]-1;
}
return B;
}
void main( )
{
int n,i,A[20],k = 0,j,B[20],*C;
printf("\nEnter the number of elements in the array :");
scanf("%d",&n);
printf("\nEnter the elements of array :\n");
for(i=1;i<=n;i++)
scanf("%d",&A[i]);
for(i=1;i<=n;i++)
if(A[i]>k)
k = A[i];
C = counting_sort(A,B,k,n);
for(i=1;i<=n;i++)
{ C++;
printf("%d ",*C);
}
}
OUTPUT
#include<stdio.h>
#include<conio.h>
void matrix_chain_order( );
void print_optimal_parens(int i,int j);
void main( )
{
int i,j;
clrscr( );
printf("\nEnter the no of matrices to be multiplied :");
scanf("%d",&n);
printf("\nEnter the dimensions of matrices :\n");
for(i=0;i<=n;i++)
scanf("%d",&p[i]);
matrix_chain_order( );
printf(“Optimal parenthesization for these matrices is :”);
print_optimal_parens(1,n);
getch( );
}
void matrix_chain_order( )
{
int i,j,l,q,k;
for(i=1;i<=n;i++)
m[i][i] = 0;
for(l=2;l<=n;l++)
for(i=1;i<=n-l+1;i++)
{
j = i+l-1;
m[i][j] = 50000;
for(k=i;k<=j-1;k++)
{
q = m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
if(q<m[i][j])
{
m[i][j] = q;
s[i][j] = k;
}
}
}
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<limits.h>
#define infinity INT_MAX
int recursive_matrix_chain(int i,int j);
void print_optimal_parens(int i,int j);
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<limits.h>
#define infinity INT_MAX
void memoized_matrix_chain( );
int look_up_chain(int i,int j);
void print_optimal_parens(int i,int j);
void memoized_matrix_chain( )
{
int i,j;
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
m[i][j] = infinity;
look_up_chain(1,n);
}
if(i==j)
m[i][j] = 0;
else
{
for(k=i;k<=j-1;k++)
{
q = look_up_chain(i,k)+look_up_chain(k+1,j)+p[i-1]*p[k]*p[j];
if(q<m[i][j])
{
m[i][j] = q;
s[i][j] = k;
}
}
}
return(m[i][j]);
}
OUTPUT
Enter the no of matrices to be multiplied :7
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[2][2],b[2][2],p[7],c[2][2],i,j,n;
clrscr( );
printf("\nEnter elements of matrix a:\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
p[0] = a[0][0]*(b[0][1]-b[1][1]);
p[1] = (a[0][0]+a[0][1])*b[1][1]);
p[2] = (a[1][0]*a[1][1])*b[0][0]);
p[3] = a[1][1]*(b[1][0]-b[0][0]);
p[4] = (a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
p[5] = (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
p[6] = (a[0][0]-a[1][0])*(b[0][0]+b[0][1]);
c[0][0] = p[4]+p[3]-p[1]+p[5];
c[0][1] = p[0]+p[1];
c[1][0] = p[2]+p[3];
c[1][1] = p[4]+p[0]-p[2]-p[6];
41 39
44 6
PROGRAM 7
#include<conio.h>
float x[10];
void knapsack( );
int m,n,i,j;
float p[10],w[10],p_w[10],temp,sum=0.0,sp=0.0;
void main( )
{
clrscr( );
printf("\nEnter the number of items :");
scanf("%d",&n);
printf("\nEnter the maximum weight that can be carried by knapsack :");
scanf("%d",&m);
printf("Enter the weight of each item item :\n");
for(i=0;i<n;i++)
scanf("%f",&w[i]);
printf("\nEnter the priority of each item :\n");
for(i=0;i<n;i++)
scanf("%f",&p[i]);
for(i=0;i<n;i++)
p_w[i]=p[i]/w[i];
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(p_w[i]<p_w[j])
{
temp = p_w[i];
p_w[i]= p_w[j];
p_w[j]=temp;
temp = w[i];
w[i]= w[j];
w[j]=temp;
temp = p[i];
p[i]= p[j];
p[j]=temp;
}
knapsack( );
printf("\nThe resulting weight,priorities,parts are :\n");
printf("Weight\t\tPriority\t\tPart\n");
for(i=0;i<n;i++)
printf("%f\t%f\t%f\n",w[i],p[i],x[i]);
printf("\nThe total weight carried by knapsack :");
for(i=0;i<n;i++)
sum=sum+x[i]*w[i];
printf("%f\n",sum);
printf("%f\n",sp);
getch( );
void knapsack( )
{
int i;
float u;
for(i=0;i<n;i++)
x[i]=0.0;
u=m;
for(i=0;i<n;i++)
if(w[i]>u)
break;
else
{
x[i]=1;
u=u-w[i];
}
if(i<n)
x[i]=u/w[i];
}
OUTPUT
Nqueen(1,n);
getch( );
}
Nqueen(int k,int n)
{
int i,j;
for(i = 1;i<=n;i++)
{
if(place(k,i))
{
x[k] = i;
if(k==n)
{
for(i=1;i<=n;i++)
printf("\t%d",i);
printf("\n");
for(i=1;i<=n;i++)
{
printf("%d\t",i);
for(j=1;j<=n;j++)
if(j==x[i])
printf("%c\t",'*');
else
printf("-\t");
printf("\n");
}
printf("\n");
}
else
Nqueen(k+1,n);
}
}
}
1 2 3 4
1 - * - -
2 - - - *
3 * - - -
4 - - * -
1 2 3 4
1 - - * -
2 * - - -
3 - - - *
4 - * - -
PROGRAM 9
#include<stdio.h>
#include<conio.h>
#define infinity 10000
void main( )
{
int w[10][10],q[10][10],i,j,k,m,n,s;
clrscr( );
printf("Enter the number of vertices :");
scanf("%d",&n);
printf("\nEnter the input weighted matrix<'-'for infinity>:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&w[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j)
q[i][j] = 0;
else
if(w[i][j]==0)
q[i][j] = infinity;
else
q[i][j] = w[i][j];
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
q[i][j] = (q[i][j]<(q[i][k]+q[k][j]))?q[i][j]:(q[i][k]+q[k][j]);
0 3 8 0 -4
0 0 0 1 7
0 4 0 0 0
2 0 -5 0 0
0 0 0 6 0
The minimum path matrix is :
0 1 -3 2 -4
3 0 -4 1 -1
7 4 0 5 3
2 -1 -5 0 -2
8 5 1 6 0
PROGRAM 8
Study and analyse Dijkstra’s algorithm for single source shortest path
in a weighted graph.
#include<stdio.h>
#include<conio.h>
#include<limits.h>
int n,k;
#define perm 1
#define tent 2
#define infinity INT_MAX
int parent(int i)
{
return i/2;
}
int left(int i)
{
return 2*i;
}
int right(int i)
{
return 2*i+1;
}
initialize_single_source(state,s,n);
build_min_heap(state,n);
while(k!=0)
{
min = heap_extract_min(state);
for(i=1;i<=k;i++)
if(a[[Link]][state[i].number]>0 && state[i].label==tent)
relax(min,a,state,i);
[Link] = perm;
}
for(i=1;i<=n;i++)
if(i!=s)
{
j = i;
dist = 0;
count = 0;
do
{
count++;
path[count] = j;
for(k=1;k<=n;k++)
if(state[k].number==j)
{
j = state[k].predecessor;
break;
}
}while(j!=0);
for(j=1;j<=count/2;j++)
{
x = path[j];
path[j] = path[count-j+1];
path[count-j+1] = x;
}
for(j=1;j<count;j++)
dist +=a[path[j]][path[j+1]];
for(i=1;i<=n;i++)
{
printf("\nEnter node %d connectivity :",i);
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
k=n;
printf("\nEnter the source node :");
scanf("%d",&source);
Dijkstra(a,n,source);
getch( );
}
OUTPUT
PRIM’S ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<limits.h>
int n,k;
#define perm 1
#define tent 2
#define infinity INT_MAX
int parent(int i)
{ return i/2;}
int left(int i)
{ return 2*i;}
int right(int i)
{ return 2*i+1;}
while(k!=0)
{
min = heap_extract_min(state);
for(i=1;i<=k;i++)
if(a[[Link]][state[i].number]>0 && state[i].label==tent)
relax(min,a,state,i);
[Link] = perm;
}
for(i=1;i<=n;i++)
if(state[i].predecessor!=0)
printf("\n%d-->%d",state[i].predecessor,state[i].number);
}
void main( )
{
int a[10][10],i,j,root;
clrscr( );
printf("Enter the number of nodes :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter node %d connectivity :",i);
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
k=n;
printf("\nEnter the root node :");
scanf("%d",&root);
Dijkstra(a,n,root);
getch( );
}
OUTPUT
Enter the number of nodes :9
Enter node 1 connectivity :0 4 0 0 0 0 8 3 0
Enter node 2 connectivity :4 0 6 8 0 0 0 0 7
Enter node 3 connectivity :0 6 0 5 4 0 0 0 1
Enter node 4 connectivity :0 8 5 0 9 0 0 0 0
Enter node 5 connectivity :0 0 4 9 0 10 3 0 6
Enter node 6 connectivity :0 0 0 0 10 0 11 0 2
Enter node 7 connectivity :8 0 0 0 3 11 0 8 1
Enter node 8 connectivity :3 0 0 0 0 0 8 0 4
Enter node 9 connectivity :0 7 1 0 6 2 1 4 0
Enter the root node :1
#include<stdio.h>
#include<conio.h>
#include<limits.h>
#define infinity INT_MIN
nodeptr getnode( )
{
nodeptr p;
p=(nodeptr) malloc(sizeof(struct node));
return p;
}
int parent(int i)
{
return i/2;
}
int left(int i)
{
return 2*i;
}
int right(int i)
{
return 2*i+1;
}
void min_heapify(int i)
{
int l,r,smallest;
l = left(i);
r = right(i);
if(l<=k && nodes[l]->data<nodes[i]->data)
smallest = l;
else
smallest = i;
if(r<=k && nodes[r]->data<nodes[smallest]->data)
smallest = r;
if(smallest!=i)
{
temp = nodes[i];
nodes[i] = nodes[smallest];
nodes[smallest] = temp;
min_heapify(smallest);
}
}
void build_min_heap(int n)
{
int i;
for(i=n/2;i>=1;i--)
min_heapify(i);
}
nodeptr heap_extract_min( )
{
if(k<1)
printf("\nHeap underflow\n");
min = nodes[1];
nodes[1] = nodes[k];
k = k-1;
min_heapify(1);
return min;
}
void min_heap_insert(nodeptr z)
{
int i;
k=k+1;
nodes[k] = z;
}
void main( )
{
int a[10],b[10],i,j=1,l,x;
char c[10];
nodeptr root = NULL,tree=NULL;
clrscr( );
root = huffmann(a,c,n);
PrintTree(root,1);
tree = root;
postorder(tree);
for(i=1;i<h;i++)
{
tree = nodes[i];
j = 0;
while(tree!=root)
{
j++;
if((tree->parent)->lst==tree)
b[j] = 0;
else
b[j] = 1;
tree = tree->parent;
}
for(l=1;l<=j/2;l++)
{
x = b[l];
b[l] = b[j-l+1];
b[j-l+1] = x;
}
printf("\nThe codeword for character %c is ",nodes[i]->character);
for(l=1;l<=j;l++)
printf("%d",b[l]);
printf("\n");
}
getch( );
}
OUTPUT
16
30
9
14
5
55
13
25
12
100
45
#include<stdio.h>
#include<conio.h>
void counting_sort(int A[ ][20],int n,int q);
int B[20][20];
int k,d;
void main( )
{
int n,i,A[20][20],j,l;
clrscr( );
printf("\nEnter the radix of elements in the array :");
scanf("%d",&k);
printf("\nEnter the number of elements in the array :");
scanf("%d",&n);
printf("\nEnter the number of digits in each element :");
scanf("%d",&d);
printf("Enter the elements of array(varying from 0 to %d):",k-1);
for(i=1;i<=n;i++)
for(j=1;j<=d;j++)
{
scanf("%d",&A[i][j]);
if(A[i][j]>k-1)
{
printf("\nNot an allowed input\n");
getch( );
exit(0);
}
}
for(i=d;i>=1;i--)
{
counting_sort(A,n,i);
for(l=1;l<=n;l++)
for(j=1;j<=d;j++)
A[l][j] = B[l][j];
}
printf("Elements in sorted array are :");
for(l=1;l<=n;l++)
{
printf("\n");
for(j=1;j<=d;j++)
printf("%d ",A[l][j]);
}
}