Algorithm Analysis and Design Practical File: Gurteg Sawhney 01416401511 B.Tech It 6 SEM Usict, Ggsipu

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

ALGORITHM

ANALYSIS AND
DESIGN
PRACTICAL FILE

GURTEG SAWHNEY
01416401511
B.TECH IT 6TH SEM
USICT ,GGSIPU

1)COUNTING SORT
#include <stdio.h>
#include<conio.h>
void counting_sort(int A[], int k, int n)
{
int i, j;
int B[15], C[100];
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;
}
printf("The Sorted array is : ");
for (i = 1; i <= n; i++)
printf("%d ", B[i]);
}

int main()
{
int n, k = 0, A[15], i;
printf("Enter the number of input : ");
scanf("%d", &n);
printf("\nEnter the elements to be sorted :\n");
for (i = 1; i <= n; i++)
{
scanf("%d", &A[i]);
if (A[i] > k) {
k = A[i];
}
}
counting_sort(A, k, n);

printf("\n");
return 0;
}

2)QUICK SORT
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
int a[6];
int partition(int x, int y)
{
int pivot,start;
int temp;
start=x;
pivot=a[x];
x++;
while(y>x)
{
while(pivot>a[x])
x++;
while(pivot<a[y])
y--;
if(x<y)
{
int temp;
temp=a[x];
a[x]=a[y];
a[y]=temp;
}
}
temp=a[y];
a[y]=pivot;
a[start]=temp;
return y;
}

void quicksort(int x, int y)


{
int p;
if(x<y)
{
p=partition(x,y);

quicksort(x,p-1);
quicksort(p+1,y);
}
}

void main()
{
clrscr();
int i;
cout<<"Enter the six elements to be quick sorted\n";
for(i=0;i<6;i++)
{
cin>>a[i];
}
quicksort(0,5);
cout<<" The sorted array is\n";
for(i=0;i<6;i++)
{
cout<<a[i]<<" ";
}
getch();
}

3)MERGE SORT
#include<stdio.h>
#include<conio.h>
#define SIZE 6
int a[SIZE],b[SIZE];
void merge_sort(int, int);
void merge(int, int, int);
void main()
{
int i;
clrscr();
for(i=0;i<SIZE;i++)
scanf("%d",&a[i]);
merge_sort(0,SIZE-1);
printf("\n\nSorted array is:\n");
for(i=0;i<SIZE;i++)
printf("%d\t",a[i]);

getch();
}
void merge_sort(int m, int n)
{
int k;
if(m<n)
{
k=(m+n)/2;
merge_sort(m,k);
merge_sort(k+1,n);
merge(m,k,n);
}
}
void merge(int m, int k, int n)
{ int i,j,l;
int L[SIZE], R[SIZE];
for(i=m;i<=k;i++)
L[i]=a[i];
for(i=k+1;i<=n;i++)
R[i]=a[i];
i=m;
j=k+1;
l=m;
while(i<=k && j<=n)
{
if(L[i]<=R[j])
{
a[l]=L[i];
i++;
}
else
{
a[l]=R[j];
j++;
}
l++;
}
while(i<=k)
{
a[l++]=L[i++];
}
while(j<=n)
{
a[l++]=R[j++];
}
}

4)RADIX SORT
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
int a[9];
int b[9];
void countingsort(int x)
{
int count[2]={0,0},bit[9],i;
for(i=1;i<9;i++)
{
bit[i]=a[i]&(1<<x);
if(bit[i]!=0)
{
bit[i]=bit[i]/bit[i];
}
}
for(i=1;i<9;i++)
{
count[bit[i]]++;
}
count[1]=count[1]+count[0];
for(i=8;i>=1;i--)
{
int j;
j=count[bit[i]];
b[j]=a[i];
count[bit[i]]--;
}
for(i=1;i<9;i++)
{
a[i]=b[i];
}
}
void radixsort()
{
int x=0,numofbits=3;
while(numofbits>0)
{
countingsort(x);
numofbits--;
x++;
}
}
void main()
{
clrscr();
int i;
cout<<"Enter 8 elements ( range 0-7) to be radix sorted\n";

for(i=1;i<9;i++)
{
cin>>a[i];
}
radixsort();
cout<<"The sorted output is: \n:";
for(i=1;i<9;i++)
{
cout<<b[i]<<", ";
}
getch();
}

5)SELECTION SORT
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], n, c, d, position, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )


scanf("%d", &array[c]);

for ( c = 0 ; c < ( n - 1 ) ; c++ )


{
position = c;

for ( d = c + 1 ; d < n ; d++ )

{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);

return 0;
}

6)BFS
#include<iostream.h>
#include<conio.h>
int queue[100];
int front=-1,end=-1;
void enqueue(int x)
{
cout<<"this is enqueue\n";
end++;
queue[end]=x;
}

int dequeue()
{
if(front<=end)
return queue[front++];
}

int isempty()
{
if(front>end)
return 1;
else
return 0;
}
main()
{
clrscr();
int root;
int
adjm[5][5]={0,1,1,1,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0};//{0,1,1,1,0,1,0,0,0,1,1,0,0,0,1,1,0,0,
0,0,0,1,1,0,0};
int ans[5][5];
int p[5]={-1,-1,-1,-1,-1},c[5]={0,0,0,0,0,},d[]={0,0,0,0,0};
cout<<"enter the root node (0,1,2,3,4)\n";
cin>>root;
c[root]=1;
enqueue(root);
while(isempty()!=1)
{
cout<<"this is while\n";
int u=dequeue();
for(int v=0;v<5;v++)
{
if(adjm[u][v]==1 && c[v]==0)
{
p[v]=u;
c[v]=1;
d[v]=d[u]+1;
enqueue(v);
}
}
c[u]=2;
}
for(int i=0;i<5;i++)
cout<<p[i]<<" ";
getch();
}

7)DFS
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("\n %d->%d",v,i);
dfs(i);
}
}
void main()
{
int i,j,count=0;
clrscr();
printf("\n Enter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;

}
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
getch();

8)PRIMS ALGORITHM
-#include<iostream.h>
#include<conio.h>
int a[5][5],p[5]={-1,-1,-1,-1,-1},key[5]={500,500,500,500,500},tempkey[5];
int q[20];
int front=-1,rear=-1;
void enqueue(int x)
{
q[++rear]=x;

if(rear==0)
front=0;
}
int dequeuemin()
{
for(i=0;i<5;i++)
tempkey[i]=key[i];
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++ )
{
if(tempkey[i]>tempkey[j])
{
int temp=tempkey[j];
tempkey[j]=tempkey[i];
tempkey[i]=temp;
}
}
}
for(i=0;i<7;i++)
{
if(key[i]=tempkey[0])
return i;
}
}
void main()

{
int i,j,root;
cout<<"enter the adjacency matrix for 5 nodes\n";
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
cin>>a[i][j];
}
cout<<"enter the root node (1,2,3,4,5)\n";
cin>>root;
p[root]=0;
key[root]=0;
for(int i=0;i<5,i++)
enqueue(i);
while(front!=rear/* while queue is not empty*/)
{
int v,u;
u=dequeuemin();
for(int v=0;v<5;v++)
{
if(a[u][v]!=0)
{
if(v is in queue and key(v)>a[u][v])
{
p[v]=u;
key[v]=a[u][v];

}
}
}
}
}

You might also like