Sunteți pe pagina 1din 15

BREADTH FIRST SEARCH

#include<iostream.h>
#include<conio.h>
int a[10][10],visited[10],n;
void dfs(int k);
void main()
{
clrscr();
cout<<"\n Enter the number of nodes";
cin>>n;
cout<<"\n give connection \n\n {for no connection = 0 connection =1}";
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j)
{
cout<<"\n\n connection from ["<<i<<"] to ["<<j<<"]:";
cin>>a[i][j];
cout<<"\n";
}
cout<<"\n\n node are visited in following order->\t";
for(j=1;j<=n;j++)
if(visited[j]==0)
dfs(j);
}

void dfs(int k)
{
int i,j,x; //" hhhhhhhhhhhhhhhhhhhhhhhhhhhh;
for(x=1;x<=n;x++)
{
if(k!=x)
getch();
}
cout<<k;
visited[k]=1;
for(i=1;i<=n;i++)
{
if(visited[j]==0)
if(a[k][i])
dfs(j);
getch();
//break;
}
}
OUTPUT:

Enter the number of nodes4

give connection

{for no connection = 0 connection =1}

connection from [1] to [2]:1

connection from [1] to [3]:1

connection from [1] to [4]:0

connection from [2] to [1]:1

connection from [2] to [3]:0

connection from [2] to [4]:1

connection from [3] to [1]:0

connection from [3] to [2]:0

connection from [3] to [4]:0

connection from [4] to [1]:0

connection from [4] to [2]:1

connection from [4] to [3]:0

node are visited in following order-> 1 2 3 4


DEPTH FIRST SEARCH

#include<iostream.h>
#include<conio.h>
int a[10][10],visited[10],n;
void bfs(int k);
void main()
{
clrscr();
cout<<"\n Enter the number of nodes";
cin>>n;
cout<<"\n give connection \n\n {for no connection = 0 connection =1}";
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j)
{
cout<<"\n\n connection from ["<<i<<"] to ["<<j<<"]:";
cin>>a[i][j];
cout<<"\n";
}
cout<<"\n\n node are visited in following order->\t";
for(i=1;i<=n;i++)
if(visited[i]==0)
bfs(i);
}

void dfs(int k)
{
int i;
cout<<k;
visited[k]=1;
for(i=1;i<=n;i++)
{
if(visited[i]==0)
if(a[k][i])
dfs(i);
getch();
//break;
}
}
OUTPUT:

Enter the number of nodes4

give connection

{for no connection = 0 connection =1}

connection from [1] to [2]:1

connection from [1] to [3]:1

connection from [1] to [4]:0

connection from [2] to [1]:1

connection from [2] to [3]:0

connection from [2] to [4]:1

connection from [3] to [1]:0

connection from [3] to [2]:0

connection from [3] to [4]:0

connection from [4] to [1]:0

connection from [4] to [2]:1

connection from [4] to [3]:0

node are visited in following order-> 1 2 4 3


INSERTION SORT

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<string.h>

class is
{
public:
void read_list(int a[],int n);
void print_list(int a[],int n);
void insert_sort(int a[],int n);
};

void is :: read_list(int a[],int n)


{
int i,j;
for(i=0;i<n;i++)
{
j = i + 1;
cout<<"\n\n\t ENTER THE ELEMENT: "<<j<<" :: ";
cin>>a[i];
}
}

void is :: print_list(int a[],int n)


{
int i;
for(i=0;i<n;i++)
cout<<" "<<a[i]<<" ";
}

void is :: insert_sort(int a[],int n)


{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0 && a[j]>temp;j--)
a[j+1]=a[j];
a[j+1]=temp;
cout<<"\n\n\t PASS:"<<i<<" :: ";
print_list(a,n);
}
}
void main()
{
is vid;
int a[20],n;
clrscr();
cout<<"\n\n\t ENTER THE ARRAY LENGTH: ";
cin>>n;
vid.read_list(a,n);
cout<<"\n\n\t THE ARRAY ELEMENTS ARE AS FOLLOWS:";
vid.print_list(a,n);
vid.insert_sort(a,n);
cout<<"\n\n\t THE SORTED LIST IS:";
vid.print_list(a,n);
getch();

}
OUTPUT

ENTER THE ARRAY LENGTH : 5

ENTER THE ELEMENT: 1 :: 5

ENTER THE ELEMENT: 2 :: 6

ENTER THE ELEMENT: 3 :: 3

ENTER THE ELEMENT: 4 :: 2

ENTER THE ELEMENT: 5 :: 1

THE ARRAY ELEMENTS ARE AS FOLLOWS: 5 6 3 2 1

PASS:1 :: 5 6 3 2 1

PASS:2 :: 3 5 6 2 1

PASS:3 :: 2 3 5 6 1

PASS:4 :: 1 2 3 5 6

THE SORTED LIST IS: 1 2 3 5 6


SELECTION SORT

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<string.h>

class sc
{
public:
void read_list(int a[],int n);
void print_list(int a[],int n);
void select_sort(int a[],int n);
};

void sc :: read_list(int a[],int n)


{
int i,j;
for(i=0;i<n;i++)
{
j = i + 1;
cout<<"\n\n\t ENTER THE ELEMENT:"<<j<<" :: ";
cin>>a[i];
}
}

void sc :: print_list(int a[],int n)


{
int i;
for(i=0;i<n;i++)
cout<<" "<<a[i]<<" ";
}

void sc :: select_sort(int a[],int n)


{
int i,j,temp,min;
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[min]>a[j])
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
cout<<"\n\n\t PASS : "<<i<<" :: " ;
print_list(a,n);
}
}
void main()
{
sc vid;
int a[20],n;
clrscr();
cout<<"\n\n\t ENTER THE ARRAY LENGTH :: ";
cin>>n;
vid.read_list(a,n);
cout<<"\n\n\t THE ARRAY ELEMENTS ARE AS FOLLOWS :: ";
vid.print_list(a,n);
vid.select_sort(a,n);
cout<<"\n\n\t THE SORTED LIST IS :: ";
vid.print_list(a,n);
getch();
}
OUTPUT

ENTER THE ARRAY LENGTH :: 5

ENTER THE ELEMENT:1 :: 6

ENTER THE ELEMENT:2 :: 5

ENTER THE ELEMENT:3 :: 25

ENTER THE ELEMENT:4 :: 7

ENTER THE ELEMENT:5 :: 2

THE ARRAY ELEMENTS ARE AS FOLLOWS :: 6 5 25 7 2

PASS : 0 :: 2 5 25 7 6

PASS : 1 :: 2 5 25 7 6

PASS : 2 :: 2 5 6 7 25

PASS : 3 :: 2 5 6 7 25

PASS : 4 :: 2 5 6 7 25

THE SORTED LIST IS :: 2 5 6 7 25


QUICK SORT

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<string.h>

class qs
{
public:
void read_list(int a[],int n);
void print_list(int a[],int n);
void quick_sort(int a[],int first,int last);
};

void qs :: read_list(int a[],int n)


{
int i,j;
for(i=0;i<n;i++)
{
j = i +1;
cout<<"\n\n\t ENTER THE ELEMENT : "<<j<< " :: ";
cin>>a[i];
}
}

void qs :: print_list(int a[],int n)


{
int i;
for(i=0;i<n;i++)
cout<<" "<<a[i]<<" ";
}

void qs :: quick_sort(int a[],int first,int last)


{
int low,high,temp,pivot;
low=first;
high=last;
pivot=a[(first+last)/2];

do
{
while(a[low]<pivot)
low++;
while(a[high]>pivot)
high--;
if(low<=high)
{
temp=a[low];
a[low]=a[high];
a[high]=temp;
low=low+1;
high=high-1;
}
}while(low<=high);

if(first<high)
quick_sort(a,first,high);
if(low<last)
quick_sort(a,low,last);
}

void main()
{
qs vid;
int a[20],n;
clrscr();
cout<<"\n\n\t ENTER THE ARRAY LENGTH :: ";
cin>>n;
vid.read_list(a,n);
cout<<"\n\n\t THE ARRAY ELEMENTS ARE AS FOLLOWS :: ";
vid.print_list(a,n);
vid.quick_sort(a,0,n-1);
cout<<"\n\n\t THE SOTED LIST IS :: ";
vid.print_list(a,n);
getch();

}
OUTPUT

ENTER THE ARRAY LENGTH :: 5

ENTER THE ELEMENT : 1 :: 6

ENTER THE ELEMENT : 2 :: 22

ENTER THE ELEMENT : 3 :: 3

ENTER THE ELEMENT : 4 :: 51

ENTER THE ELEMENT : 5 :: 0

THE ARRAY ELEMENTS ARE AS FOLLOWS :: 6 22 3 51 0

THE SOTED LIST IS :: 0 3 6 22 51


MAX MIN ALGORITHM

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],size,max,min;
cout<<"enter the size of the array?";
cin>>size;
cout<<"enter the elements in an array?";
for(int i=0;i<size;i++)
{
cin>>a[i];
max=a[0];
}
for(i=0;i<size;i++)
{
if(a[i]>max)
{
max=a[i];
min=a[0];
}
}
for(i=0;i<size;i++)
{
if(a[i]<min)
min=a[i];
}
cout<<"\n the max and min elements are"<<max<<" "<<min;
getch();
}
OUTPUT

enter the size of the array?4


enter the elements in an array?
22
33
44
12

the max and min elements are44 12

S-ar putea să vă placă și