Programs ds.1
Programs ds.1
Programs ds.1
#include<stdio.h>#include<conio.h>int Fibonacci(int);voidmain()
int i,n;clrscr();
printf("\nEnterthenoofelementstobedisplayed:");scanf("%d",&n);
for(i=0;i<n;i++)printf("%d\t",Fibonacci(i));getch();
intFibonacci(intn)
if(n<=0)return0;
else if (n==1)return1;
else
returnFibonacci(n-1)+Fibonacci(n-2);
4. Aim: Implementationofcircularqueueusingarray.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int x;
char ch='1';clrscr();while(ch!='4')
{
ch=getchar();switch(ch)
case'1':
printf("\nEnterthenosofelement tobeinserted:");scanf("%d",&x);
Insert(x);break;case'2':
x=Delete();
case'3':
Display();break;case'4':
break;default:
printf("\nWrongchoice!Tryagain:");
getch();
intIsempty()
if(front==-1)return1;else
return0;
intIsfull()
{
if(front==(rear+1)%MAXQ)return1;
elsereturn0;
voidInsert(intx)
if(Isfull())
if(front==-1)
front=0;rear=0;
elserear=(rear+1)%MAXQ;items[rear]=x;
intDelete()
int x;if(Isempty())
x=items[front];if(front==rear)
{
front=-1;rear=-1;
elsefront=(front+1)%MAXQ;returnx;
voidDisplay()
int i,n;if(Isempty())
for(i=front;i<=rear;i++)printf("%d\n",items[i]);
else
for(i=front;i<=MAXQ-1;i++)printf("%d\n",items[i]);for(i=0;i<=rear;i++)printf("%d\n",items[i]);
}
OUTPUT
5. Aim : Implementation of binary search tree using array.
#include<stdio.h>#include<conio.h>#include<stdlib.h>#defineTRUE1
#defineTREENODES100
int info;intused;
};
structtreenode[TREENODES];voidCreatetree();
void Insert(int);voidDisplay();voidSetleft(int,int);
void Setright(int,int);voidmain()
int x;
char ch='1';clrscr();
printf("\nEnterrootnodevalue:");scanf("%d",&x);
Createtree(x);while(ch!='3')
printf("\n1-INSERT");printf("\n2-DISPLAY");printf("\n3-QUIT");
ch=getchar();switch(ch)
case '1':
Insert(x);break;case '2':Display();break;
case'3':
break;default:
printf("\nWrongchoice!Tryagain:");
voidCreatetree(intx)
int i;node[0].info=x;
node[0].used=TRUE;for(i=1;i<TREENODES;i++)
node[i].used=FALSE;
voidInsert(intx)
int p,q;p=q=0;
while(q<TREENODES&&node[q].used&&x!=node[p].info)
p=q;if(x<node[p].info)q=2*p+1;
elseq=2*p+2;
if(x==node[p].info)
if(x<node[p].info)Setleft(p,x);
elseSetright(p,x);
voidSetleft(intpos,int x)
int q;q=2*pos+1;
if(q>TREENODES)
node[q].info=x;
node[q].used=TRUE;
voidSetright(intpos,intx)
int q;q=2*pos+2;
if(q>TREENODES)
node[q].info=x;node[q].used=TRUE;
voidDisplay()
{
int i;for(i=0;i<TREENODES;i++)
if(node[i].used==TRUE)printf("%d ",node[i].info);printf("\n");
output
6. REPRESENTATION OF GRAPH
Algorithm
• For a graph with |V| vertices, an adjacency matrix is a |V|×∣V∣ matrix of 0s and 1s, where
the entry in row i and column j is 1 if and only if the edge (i,j) in the graph.
3. If the edge connects the vertices i,j the mark the ith row and jth column of the adjacency
matrix as 1 otherwise store 0
Program
#include<stdio.h>
#define V 5
int i,j;
arr[i][j] = 0;
arr[src][dest] = 1;
int i, j;
{
for(j = 0; j < V; j++)
printf("\n");
int main()
int adjMatrix[V][V];
printAdjMatrix(adjMatrix);
return 0;
OUTPUT
01110
00011
00010
00001
00000
7. A) Quick Sort
QUICK SORT
Aim:To sort an array of N numbers using Quick sort.
Algorithm
1. Start
8. Stop
Program
#include<stdio.h> #include<conio.h>
{
pivot = fst; i = fst;
i++;
if(i <j )
tmp = arr[pivot]; arr[pivot] = arr[j]; arr[j] = tmp; qsort(arr, fst, j-1); qsort(arr, j+1, last);
Output
127
-1
04
-2
Result
Thus the array was sorted using quick sort‟s divide and conquers method.
b) Selection Sort
#include<stdio.h>#include<conio.h>
voidSelectionsort(int[],int);voidmain()
int x[20],i,n;clrscr();
getch();
voidSelectionsort(inta[],intn)
int i,j,pos,large;for(i=n-1;i>0;i--)
large=a[0];pos=0;for(j=1;j<=i;j++)
if(a[i]>large)
large=a[j];pos=j;
a[pos]=a[i];a[i]=large;
}
Output:
8. A. Sequential Search
#include<stdio.h>#include<conio.h>
intSequentialsearch(int[],int,int);voidmain()
intx[20],i,n,p,key;clrscr();
if(p==-1)
intSequentialsearch(inta[],intn,intk)
int i;for(i=0;i<n;i++)
if(k==a[i])return(i);
return(-1);
Output
8.B. Binary Search
BINARY SEARCH
Aim
Algorithm
1. Start
ii. Stop
i. lower = mid + 1
d. else
i. upper = mid – 1
8. Stop
Program
#include <stdio.h>
void main()
int a[50],i, n, upper, lower, mid, val, found, att=0; printf("Enter array size : ");
printf("%4d", a[i]);
upper = n; lower = 0;
found = -1;
if (a[mid] == val)
lower = mid + 1;
Output
Result
9. A) Heap Sort
#include<stdio.h>
#include<conio.h>
int Left(int);intRight(int);
int x[20],i,n;clrscr();
printf("\nEnter%delements:",n);
for(i=0;i<n;i++)scanf("%d",&x[i]);Heapsort(x,n);
getch();
intParent(inti)
return(i/2);
int Left(inti)
return(2*i+1);
intRight(inti)
return(2*i+2);
voidHeapify(inta[],inti,intn)
if((l<=n-1)&&(a[l]>a[i]))large=l;
elselarge=i;
if((r<=n-1)&&(a[r]>a[large]))large=r;
if(large!=i)
{
temp=a[i];a[i]=a[large];a[large]=temp;Heapify(a,large,n);
voidBuildheap(inta[],intn)
inti;
for(i=(n-1)/2;i>=0;i--)Heapify(a,i,n);
voidHeapsort(inta[],intn)
int i,m,temp;Buildheap(a,n);m=n;
for(i=n-1;i>=1;i--)
temp=a[0];a[0]=a[i];a[i]=temp;m=m-1;
Heapify(a,0,m);
Output
9. B. Radix sort
#include<stdio.h>#include<conio.h>
voidRadixsort(int[],int);voidmain()
int x[20],i,n;clrscr();
getch();
voidRadixsort(inta[],intn)
{
int bucket[10][10],buck[10];int i,j,k,l,num,div,large,pass;div=1;
num=0;large=a[0];for(i=0;i<n;i++)
if(a[i]>large)large=a[i];
while(large>0)
num=num+1;large=large/10;
for(pass=0;pass<num;pass++)
for(k=0;k<10;k++)buck[k]=0;for(i=0;i<n;i++)
l=(a[i]/div)%10;bucket[l][buck[l]++]=a[i];
}i=0;
for(k=0;k<10;k++)
for(j=0;j<buck[k];j++)a[i++]=bucket[k][j];
div=div*10;
Output: