0% found this document useful (0 votes)
10 views19 pages

Null 9

Uploaded by

Gokulakrishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

Null 9

Uploaded by

Gokulakrishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

BINARY SEARCH
Program:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int count,i,arr[30],num,first,last,middle;
cout<<"How many Element would you like to Enter: ";
cin>>count;
for(i=0;i<count;i++)
{
cout<<"Enter The Element"<<(i+1)<<":";
cin>>arr[i];
}
cout<<"Enter Element would you want to search:";
cin>>num;
first=0;
last=count-1;
middle=(first+last)/2;
while(first<=last)
{
if(arr[middle]<num)
{
first=middle+1;
}
else if(arr[middle]==num)
{
cout<<num<<" Found in the Array at the location
"<<middle+1<<"\n";
break;
}
else
{
last=middle-1;
}
middle=(first+last)/2;
}
if(first>last)
{
cout<<num<<"Not found in the array";
}
getch();
return 0;
}

Output:
2.PUSH AND POP
Program:

#include<iostream.h>
#include<conio.h>
int stack[100],n=100,top=-1;
void push(int val)
{
if(top>=n-1)
cout<<"Stack overflow"<<endl;
else
{
top++;
stack[top]=val;
}
}
void pop()
{
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else
{
cout<<"The popped Element is"<<stack[top]<<endl;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"stack Elements are";
for(int i=top;i>=0;i--)
cout<<stack[i]<<" ";
cout<<endl;
}
else
cout<<"stack is empty";
}
int main()
{
int ch,val;
cout<<"1.Push in Stack"<<endl;
cout<<"2.Pop from Stack"<<endl;
cout<<"3.Display stack"<<endl;
cout<<"4.Exit"<<endl;
do
{
cout<<"Enter Choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
cout<<"Exit"<<endl;
break;
}
default:
{
cout<<"Invalid choice"<<endl;
}
}
}
while(ch!=4);
return 0;
getch();
}
Output:
3.ENQUEUE AND DEQUEUE

Program:

#include<stdio.h>
#include<conio.h>
#define n 3
void main()
{
int queue[n],x=n;
int ch=1,j=1,i;
int front=0,rear=0;
clrscr();
printf("\n Queue Operations:");
printf("\n-----------------");
printf("\n1.Enqueue\n 2.Dequeue\n 3.Display\n 4.Exit");
while(ch)
{
printf("\n Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("Queue is Full");
else
{
printf("\n Enter the values%d:",j++);
scanf("%d",& queue[rear++]);
}
break;
case 2:
if(front==rear)
printf("\n Queue is Empty");
else
{
printf("\n The Deleted Element is:%d",queue[front++]);
x++;
}
break;
case 3:
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front;i<rear;i++)
{
printf("%d\t",queue[i]);
}
}
break;
case 4:
{
return;
}
break;
default:
printf("\n Invalid Choice");
}
}
Output:
4.BINARY TREE TRAVERSALS

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct treerec
{
struct treerec*lchild;
struct treerec*rchild;
int data;
};
struct treerec*item;
struct treerec*rdata[50];
inorder(struct treerec*cnode)
{
if(cnode!=NULL)
{
inorder(cnode->lchild);
printf("%d\t",cnode->data);
inorder(cnode->rchild);
}
return(1);
}
preorder(struct treerec*cnode)
{
if(cnode!=NULL)
{
printf("%d\t",cnode->data);
preorder(cnode->lchild);
preorder(cnode->rchild);
}
return(1);
}
postorder(struct treerec*cnode)
{
if(cnode!=NULL)
{
postorder(cnode->lchild);
postorder(cnode->rchild);
printf("%d\t",cnode->data);
}
return(1);
}
void main()
{
int i,n;
clrscr();
printf("\n \t tree Traversal");
printf("\n\t -------------------");
printf("\n Enter the nuber or elements:");
scanf("%d",&n);
printf("\n Enter the datas");
for(i=1;i<=n;i++)
{
rdata[i]=(struct treerec*)malloc(sizeof(struct treerec));
scanf("%d",& rdata[i]->data);
rdata[i]->lchild=NULL;
rdata[i]->rchild=NULL;
}
for(i=1;i<n;i++)
{
rdata[i]->lchild=rdata[2*i];
rdata[i]->rchild=rdata[(2*i)+1];
}
item=rdata[1];
printf("\n inorder:");
inorder(item);
printf("\n preorder:");
preorder(item);
printf("\n postorder:");
postorder(item);
getch();
}
Output:
5.BREADTH FIRST SEARCH
Program:

#include<stdio.h>
#include<conio.h>
int arr[10][10],visit[10],assign[10];
int i,j,k,n,v;
int f,r,queue[10];
void main()
{
int m;
clrscr();
printf("\n Enter The Number of Vertices and edges:");
scanf("%d%d",&n,&m);
printf("\n Number of Edges are:\n");
for(k=1;k<=m;k++)
{
scanf("%d%d",&i,&j);
arr[i][j]=1;
}
printf("\n Enter the value of initial vertex:");
scanf("%d",&v);
printf("\n Order of visited vertex:\n %d",v);
assign[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(arr[v][j]!=0 &&assign[j]!=1 &&visit[j]!=1)
{
visit[j]=1;
queue[r++]=j;
}
v=queue[f++];
printf("\t%d",v);
k++;
visit[v]=0;
assign[v]=1;
}
getch();
}
Output:
6.DEPTH FIRST SEARCH
Program:
#include<stdio.h>
#include<conio.h>
int arr[10][10],visit[10],assign[10];
int i,j,k,n,v;
int top,stack[10];
void main()
{
int m;
printf("\n Enter the number of Vertices and Edges:");
scanf("%d%d",&n,&m);
printf("\n Number of edges are:\n");
for(k=1;k<=m;k++)
{
scanf("%d%d",&i,&j);
arr[i][j]=1;
}
printf("\n Enter the value of initial vertex:");
scanf("%d",&v);
printf("\n Order of visited vertex:\n %d",v);
assign[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(arr[v][j]!=0&&assign[j]!=1&&visit[j]!=1)
{
visit[j]=1;
stack[top]=j;
top++;
}
v=stack[--top];
printf("\t%d",v);
k++;
visit;
visit[v]=0;
assign[v]=1;
}
getch();
}
Output:
7.MERGE SORT
Program:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void merge(int arr[], int l, int m, int r)
{
int i,j,k;
int n1=m-l+1;
int n2=r-m;
int L[100],R[100];
for(i=0;i<n1;i++)
L[i]=arr[l+i];
for(j=0;j<n2;j++)
R[j]=arr[m+1+j];
i=0;
j=0;
k=l;
while(i<n1&&j<n2){
if(L[i]<=R[j]){
arr[k]=L[i];
i++;
}
else{
arr[k]=R[j];
j++;
}
k++;
}
while(i<n1){
arr[k]=L[i];
i++;
k++;
}
while(j<n2){
arr[k]=R[j];
j++;
k++;
}
}
void mergesort(int arr[], int l, int r)
{
if(l<r){
int m=(l+r)/2;
mergesort(arr,l,m);
mergesort(arr,m+1,r);
merge(arr,l,m,r);
}
}
void printArray(int A[],int size)
{
int i;
for(i=0;i<size;i++)
printf("%d\t",A[i]);
printf("\n");
}
void main()
{
int arr[]={ 50, 8, 9, 40, 5, 6, 7 };
int arr_size=sizeof(arr)/sizeof(arr[0]);
clrscr();
printf("Given Array is -\n");
printArray(arr,arr_size);
mergesort(arr,0,arr_size-1);
printf("\n Sorted Array is-\n");
printArray(arr,arr_size);
getch();
}

Output:
8.QUICK SORT
Program:

#include<stdio.h>
#include<conio.h>
void quicksort(int a[25],int first,int last)
{
int i,j,pivot,temp;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(a[i]<=a[pivot]&&i<last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}
}
void main()
{
int i,n,a[25];
clrscr();
printf("\nEnter The Number of Elemens:");
scanf("%d",&n);
printf("\nEnter The values:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("\nSorted Elements:\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}

Output:

You might also like