Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

S.

NO DATE NAME OF THE EXPERIMENT PAGE MARK SIGNATURE


NO
1
LINEAR SEARCH
2 EVALUATION OF ARITHEMETIC
EXPRESSION

3 IMPLEMENTATION OF STACK

4 QUEUE IMPLEMENTATION

5 SINGLY LINKED LIST

6 DOUBLY LINKED LIST

7
CIRCULAR LINKED LIST
8 TREE TRAVERSAL
9 GRAPH TRAVERSAL TECHNIQUES
10 DIJASKTRA’S ALGORITHM

11 BINARY SEARCH USING DIVIDE AND


CONQUER TECHNIQUE
12 QUICK SORT

13 MERGE SORT
14 KNAPSACK PROBLEM
15
TRAVELING SALESMAN PROBLEM
16
8 QUEENS PROBLEM
LINEAR SEARCH

#include <iostream>
using namespace std;
int main()
{
int array[100], search, c, n;
cout<<"\n"<<"\t"<<"\t"<<"LINEAR SEARCH";
cout<<"\n"<<"\t"<<"\t"<<"*----------------------*";
cout<<"\n"<<"Enter the number of elements in array : ";
cin>>n;
cout<<"Enter "<<n<<" integer(s) : ";

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


cin>>array[c];
cout<<"\n"<<"Enter the number to search : ";
cin>> search;

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


{
if (array[c] == search) /* if required element found */
{
cout<<search <<"is present at location "<< c+1;
break;
}
}
if (c == n)
cout<<search<<"is not present in array";
return 0;
}
Input Output :-
LINEAR SEARCH
*---------------------*
Enter the number of elements in array : 7
Enter 7 integer(s) : 12 -14 76 44 83 21 07
Enter the number to search : 21
21 is present at location 6.
EVALUATION OF ARITHEMETIC EXPRESSION

#define SIZE 50
#include <iostream>
using namespace std;
char s[SIZE];
int top=-1;
char push(char elem)
{
s[++top]=elem;
}
char pop()
{
return(s[top--]);
}
int pr(char elem)
{
switch(elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}

main()
{
char infx[50],pofx[50],ch,elem;
int i=0,k=0;
cout<<"\n"<<"Infix to Postfix Expression";
cout<<"\n"<<"*--------------------------*";
cout<<"\n"<<"\nRead the Infix Expression ? ";
cin>>infx;
push('#');
while( (ch=infx[i++]) != '\0')
{
if( ch == '(') push(ch);
else
if(isalnum(ch)) pofx[k++]=ch;
else
if( ch == ')')
{
while( s[top] != '(')
pofx[k++]=pop();
elem=pop();
}
else
{
while( pr(s[top]) >= pr(ch) )
pofx[k++]=pop();
push(ch);
}
}
while( s[top] != '#')
pofx[k++]=pop();
pofx[k]='\0';
cout<<"\n"<<" Given Infix Expn: \n"<<" "<<infx<<"\n"<<" Postfix Expn: "<<"\n"<<"
"<<pofx; }
Input Output :-
Infix to Postfix Expression
*--------------------------*
Read the Infix Expression a+b/c
Given Infix Expression: a+b/c
Postfix Expression: abc/+
IMPLEMENTATION OF STACK

#include <iostream>
using namespace std;
#define size 3
struct stack {
int s[size];
int top;
} st;

int stfull() {
if (st.top >= size - 1)
return 1;
else
return 0;
}

void push(int item) {


st.top++;
st.s[st.top] = item;
}

int stempty() {
if (st.top == -1)
return 1;
else
return 0;
}

int pop() {
int item;
item = st.s[st.top];
st.top--;
return (item);
}

void display() {
int i;
if (stempty())
cout<<"\n Stack Is Empty! \n";
else {
cout<<"\n The Stack is \n";
for (i = st.top; i >= 0; i--)
cout<<" "<<st.s[i]<<"\n" ;
}
}

int main() {
int item, choice;
char ans;
st.top = -1;

cout<<"\n\t\tImplementation of Stack";
cout<<"\n Main Menu\n";
cout<<"\n 1. Push \n 2. Pop \n 3. Display \n 4. Exit\n";
do
{
cout<<"\n Enter Your Choice ";
cin>>choice;
switch (choice) {
case 1:
cout<<"\n Enter the element to be pushed ";
cin>>item;
if (stfull())
{
cout<<"\n Stack is Full !! \n";
break;
}
else
{
push(item);
cout<<"\n The given element "<<item<<" is pushed into the Stack \n";
break;
}
break;
case 2:
if (stempty())
cout<<"\n Empty stack! Underflow!! \n";
else {
item = pop();
cout<<"\n The popped element is "<<item<<"\n";
}
break;
case 3:
display();
break;
case 4:
break;
}
}while(choice<4);
}
Input Output :-
Implementation of Stack
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter Your Choice 1
Enter the element to be pushed 11
The given element 11 is pushed into the Stack
Enter Your Choice 1
Enter the element to be pushed 22
The given element 22 is pushed into the Stack
Enter Your Choice 1
Enter the element to be pushed 33
The given element 33 is pushed into the Stack
Enter Your Choice 1
Enter the element to be pushed 44
Stack is Full !!
Enter Your Choice 3
The Stack is
33
22
11
Enter Your Choice 2
The popped element is 33
Enter Your Choice 2
The popped element is 22
Enter Your Choice 2
The popped element is 11
Enter Your Choice 2
Empty stack! Underflow!!
Enter Your Choice 3
Stack Is Empty!
Enter Your Choice 4
QUEUE IMPLEMENTATION

#include <iostream>
using namespace std;
#define MAX 10
int queue[MAX],front=-1,rear=-1;
void insert_element();
void delete_element();
void display_queue();
int main()
{
int option;
cout<<"\n Queue Implementation";
cout<<"\n*--------------*";
do
{
cout<<"\n"<<"\n"<<" 1.Insert";
cout<<"\n"<<" 2.Delete";
cout<<"\n"<<" 3.Display";
cout<<"\n"<<" 4.Exit";
cout<<"\n"<<" Enter your choice: ";
cin>>option;
switch(option)
{
case 1: insert_element();
break;
case 2: delete_element();
break;
case 3: display_queue();
break;
case 4: break;
}
}while(option!=4);
}

void insert_element()
{
int num;
cout<<"\n"<<" Enter the number to be inserted: ";
cin>>num;
if(front==0 && rear==MAX-1)
cout<<"\n"<<" Queue OverFlow Occured";
else if(front==-1&&rear==-1)
{
front=rear=0;
queue[rear]=num;
}
else if(rear==MAX-1 && front!=0)
{
rear=0;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}

void delete_element()
{
int element;
if(front==-1)
{
cout<<"\n"<<" Underflow";
}
element=queue[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
cout<<"\n "<<"The deleted element is: "<<element;
}
}

void display_queue()
{
int i;
if(front==-1)
cout<<"\n"<<" No elements to display";
else
{
cout<<"\n"<<" The queue elements are:"<<"\n ";
for(i=front;i<=rear;i++)
{
cout<<"\t "<<queue[i];
}
}
}
Input Output :-
Queue Implementation
*--------------------------*
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the number to be inserted: 56
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the number to be inserted: 34
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the number to be inserted: -6
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 3
The queue elements are:
56 34 -6
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 2
The deleted element is: 56
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 4
SINGLY LINKED LIST

#include <iostream>
using namespace std;
struct sll
{
int data;
struct sll*link;
};
typedef struct sll node;
node*head=NULL,*temp,*p,*prev,size;
void insert();
void delte();
void display();
int main()
{
int ch;
cout<<"\n"<<" Singly Linked List";
cout<<"\n"<<" ------------------ ";
cout<<"\n "<<" MENU";
cout<<"\n"<<" 1. Insert\n 2. Delete\n 3. Display\n 4. Exit \n";
do
{
cout<<"\n "<<" Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
delte();
break;
case 3:
display();
break;
case 4:
break;
}

}while(ch<4);
}

void insert()
{
temp= new node;
cout<<"\n "<<" Enter the data ";
cin>>temp->data;

if(head==NULL)
{
head=temp;
temp->link=NULL;
cout<<"\n The given element "<<temp->data<<" is added to the list \n" ;
}
else
{
for(p=head,prev=head;p!=NULL;prev=p,p,p=p->link)
{
if(p->data>temp->data)
{
if(p==head)
{
temp->link=head;
head=temp;
break;
}
else
{
temp->link=prev->link;
prev->link=temp;
break;
}
}
}
if(p==NULL)
{
prev->link=temp;
temp->link=NULL;
cout<<"\n The given element "<<temp->data<<" is added to the list \n ";
}
}
}
void delte()
{
int x,f=0;
cout<<"\n Enter the data to be deleted ";
cin>>x;
if(head->data==x)
{
head=head->link;
f=1;
}
else
for(p=head;p!=NULL;prev=p,p=p->link)
if(p->data==x)
{
prev->link=p->link;
f=1;
break;
}
if(f==1)
cout<<"\n The selected element "<<x<<" is deleted from the list \n";
else
cout<<"\n The selected element "<<x<<" is not found in the list \n";
}
void display()
{
cout<<"\n"<<" The list is : ";
for(p=head;p!=NULL;p=p->link)
{
cout<<p->data;
if(p->link!=NULL)
cout<<"->";
}
cout<<" \n";
}
Input Output :-
Singly Linked List
------------------
MENU
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the data 12
The given element 12 is added to the list
Enter your choice: 1
Enter the data 23
The given element 23 is added to the list
Enter your choice: 1
Enter the data 34
The given element 34 is added to the list
Enter your choice: 3
The list is : 12->23->34
Enter your choice: 2
Enter the data to be deleted 44
The selected element 44 is not found in the list
Enter your choice: 2
Enter the data to be deleted 23
The selected element 23 is deleted from the list
Enter your choice: 3
The list is : 12->34
Enter your choice: 4
DOUBLY LINKED LIST

#include <iostream>
using namespace std;
typedef struct Node
{
int data;
struct Node *next;
struct Node *prev;
}node;
void insert(node *pointer, int data)
{
while(pointer->next!=NULL)
{
pointer = pointer -> next;
}
pointer->next = new node ;
(pointer->next)->prev = pointer;
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
cout<<"\n The given element "<<data<<" is inserted into the List \n";
}
void delete1(node *pointer, int data)
{
while(pointer->next!=NULL && (pointer->next)->data != data)
{
pointer = pointer->next;
}
if(pointer->next==NULL)
{
cout<<"\n The given element "<<data<<" is not present in the list \n";
return;
}
node *temp;
temp = pointer -> next;
pointer->next = temp->next;
temp->prev = pointer;
cout<<"\n The given element "<<data<<" is deleted from the List \n";
delete temp;
}
void printmylist(node *pointer)
{
if(pointer==NULL)
{
return;
}
cout<<"<->"<<pointer->data;
printmylist(pointer->next);
}
int main()
{ int cont=1;
int data;
node *start,*temp;
start = new node;
temp = start;
temp -> next = NULL;
temp -> prev = NULL;
cout<<"\n \t \t "<<" DOUBLY LINKED LIST";
cout<<"\n \t \t "<<" ------------------";
cout<<"\n \t "<<" Main Menu \n";
cout<<"\n \t "<<" 1. Insert";
cout<<"\n \t "<<" 2. Delete";
cout<<"\n \t "<<" 3. Print";
cout<<"\n \t "<<" 4. Exit \n";
while(cont)
{
int ch;
cout<<"\n"<<" Enter your Choice ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Enter the element to insert: ";
cin>>data;
insert(start,data);
break;
case 2:
cout<<"\n Enter the element to delete : ";
cin>>data;
delete1(start,data);
break;
case 3:
cout<<"\n The list is ";
printmylist(start->next);
cout<<"\n";
break;
case 4:
cont=0;break;
}
}}
Input Output :-

DOUBLY LINKED LIST


-------------------------------
Main Menu
1. Insert
2. Delete
3. Print
4. Exit
Enter your Choice 1
Enter the element to insert: 5
The given element 5 is inserted into the List
Enter your Choice 1
Enter the element to insert: 6
The given element 6 is inserted into the List
Enter your Choice 1
Enter the element to insert: 77
The given element 77 is inserted into the List
Enter your Choice 3
The list is <->5<->6<->77
Enter your Choice 2
Enter the element to delete : 88
The given element 88 is not present in the list
Enter your Choice 2
Enter the element to delete : 5
The given element 5 is deleted from the List
Enter your Choice 3
The list is <->6<->77
Enter your Choice 4
CIRCULAR LINKED LIST
#include<iostream>
#include<malloc.h>
using namespace std;

struct Node{
int data;
struct Node*next;
};
struct Node*head = NULL;
void insert(int newdata){
struct Node*newnode =(struct Node*) malloc(sizeof(struct Node));
struct Node*ptr = head;
newnode->data = newdata;
newnode->next= head;
if(head!= NULL){
while(ptr->next!= head)
ptr = ptr->next;
ptr->next= newnode;
}else
newnode->next= newnode;
head = newnode;
}
void display(){
struct Node* ptr;
ptr = head;
do{
cout<<ptr->data <<" -> ";
ptr = ptr->next;
}while(ptr != head);
}
int main(){
insert(10);
insert(16);
insert(7);
insert(2);
insert(9);
cout<<" \n CIRCULAR LINKED LIST \n";
cout<<"\n The circular linked list is: \n \n";
display();
return 0;
}
Input Output:-

CIRCULAR LINKED LIST

The circular linked list is:

9 -> 2 -> 7 -> 16 -> 10 ->


TREE TRAVERSAL

#include<iostream>
#include<stdio.h>
using namespace std;
typedef struct tree
{
int info;
struct tree *left;
struct tree *right;
}tree;
tree *root;
int x;
void Buildtree(tree*,int);
void travlnorder(tree*);
void travpreorder(tree*);
void travpostorder(tree*);
main()
{
cout<<"\nBINARY TREE TRAVERSAL USING RECURSION";
cout<<"\n*--------------------------------*";
cout<<"\nEnter the root values:";
cin>>x;
root=new tree;
root->info=x;
root->left=NULL;
root->right=NULL;
cout<<"Enter the values^zto terminate:";
while(scanf("%d",&x)!=EOF)
Buildtree(root,x);
cout<<"\n";
cout<<" \n Inorder \n";
travlnorder(root);
cout<<"\n \n Preorder \n";
travpreorder(root);
cout<<"\n \n Postorder \n";
travpostorder(root);
}
void Buildtree(tree *node,int number)
{
tree *new_node;
if(number>node->info)
{
if(node->right==NULL)
{
new_node=new tree;
new_node->info=number;
new_node->left=NULL;
new_node->right=NULL;
node->right=new_node;
}
else
Buildtree(node->right,number);
}
else
{
if(number<node->info)
if(node->left==NULL)
{
new_node=new tree;
new_node->info=number;
new_node->left=NULL;
new_node->right=NULL;
node->left=new_node;
}
else
Buildtree(node->left,number);
else
cout<<"\nduplicate number="<<number;
}
}

void travlnorder(tree*node)
{
if(node!=NULL)
{
travlnorder(node->left);
cout<<(" ");
cout<<node->info;
travlnorder(node->right);
}
}

void travpreorder(tree *node)


{
if(node!=NULL)
{
cout<<(" ");
cout<<node->info;

travpreorder(node->left);
travpreorder(node->right);
}
}
void travpostorder(tree*node)
{
if(node!=NULL)
{
travpostorder(node->left);
travpostorder(node->right);
cout<<(" ");
cout<<node->info;
}
}
Input Output:-
BINARY TREE TRAVERSAL USING RECURSION
*--------------------------------*
Enter the root values:66
Enter the values^zto terminate:22
11
33
44
55
77
88
99
^Z

Inorder
11 22 33 44 55 66 77 88 99

Preorder
66 22 11 33 44 55 77 88 99

Postorder
11 55 44 33 22 99 88 77 66
GRAPH TRAVERSAL TECHNIQUES

#include <iostream>
using namespace std;
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete1();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();
int main()
{
int n,i,s,ch,j;
char c,dummy;
cout<<"\n\t\t"<<"DFS&BFS";
cout<<"\n\t\t"<<"~~~~~~~~~";
cout<<"\n Enter the number of vertices ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<" Enter 1 if " <<i<<" has a node with "<<j<<" else 0 ";
cin>>a[i][j];
}
}
cout<<"\n The adjacency matrix is \n \n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<(" ");
cout<<a[i][j];
}
cout<<"\n";
}
do
{
for(i=1;i<=n;i++)
vis[i]=0;
cout<<"\n"<<"\t Menu";
cout<<"\n"<<"\t B.F.S";
cout<<"\n"<<"\t D.F.S";
cout<<"\n \n"<<" Enter your choice ";
cin>>ch;
cout<<"\n Enter the source vertex: ";
cin>>s;
switch(ch)
{
case 1:
bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
cout<<"\n Do you want continue (y/n)?";
//cin>>dummy;
cin>>c;
}
while((c=='y')||(c=='y'));
}
void bfs(int s,int n)
{
int p,i;
add(s);
vis[s]=1;
p=delete1();
if(p!=0)
cout<<" ";
cout<<p;
cout<<" ";
while(p!=0)
{
for(i=1;i<=n;i++)
if((a[p][i]!=0)&&(vis[i]==0))
3 {
add(i);
vis[i]=1;
}
p=delete1();
if(p!=0)
cout<<" ";
cout<<p;
cout<<" ";
}
for(i=1;i<=n;i++)
if(vis[i]==0)
bfs(i,n);
}
void add(int item)
{
if(rear==19)
{
cout<<" ";
cout<<"queue full";
}
else
{
if(rear==-1)
{
q[++rear]=item;
front++;
}
else
q[++rear]=item;
}
}
int delete1()
{
int k;
if((front>rear)||(front==-1))
return(0);
else
{
k=q[front++];
return(k);
}
}
void dfs(int s,int n)
{
int i,k;
push(s);
vis[s]=1;
k=pop();
if(k!=0)
cout<<" ";
cout<<k;
cout<<" ";
while(k!=0)
{
for(i=1;i<=n;i++)
if((a[k][i]!=0)&&(vis[i]==0))
{
push(i);
vis[i]=1;
}
k=pop();
if(k!=0)
cout<<" ";
cout<<k;
cout<<" ";
}
for(i=1;i<=n;i++)
if(vis[i]==0)
dfs(i,n); }
void push(int item)
{
if(top==19)
cout<<"stack ovwrflow";
else stack[++top]=item;
}
int pop()
{
int k;
if(top==-1)
return(0);
else
{
k=stack[top--];
return(k);
}
}
Input Output:-
DFS&BFS
~~~~~~~~~
Enter the number of vertices 3
Enter 1 if 1 has a node with 1 else 0 0
Enter 1 if 1 has a node with 2 else 0 1
Enter 1 if 1 has a node with 3 else 0 1
Enter 1 if 2 has a node with 1 else 0 1
Enter 1 if 2 has a node with 2 else 0 0
Enter 1 if 2 has a node with 3 else 0 1
Enter 1 if 3 has a node with 1 else 0 1
Enter 1 if 3 has a node with 2 else 0 1
Enter 1 if 3 has a node with 3 else 0 0

The adjacency matrix is

0 1 1
1 0 1
1 1 0

Menu
B.F.S
D.F.S

Enter your choice 1

Enter the source vertex: 3


3 1 2 0
Do you want continue (y/n)?y
Menu
B.F.S
D.F.S

Enter your choice 2

Enter the source vertex: 3


3 2 1 0
Do you want continue (y/n)?n
DIJASKTRA’S ALGORITHM

#define INFINITY 9999


#include<iostream>
using namespace std;
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main( )
{
int G[MAX][MAX],i,j,n,noofedges,u,v,w;
cout<<”\n\n”<<”\t\t”<<”Dijasktra’s Algorithm”;
cout<<”\n\n”<<”\t\t”<<”*-----------------------*”;
cout<<”\n\n\t”<<”Enter no.of vertices:”;
cout>>n;
cout<<”\n\n\t”<<”No.of edges:”;
cin>>noofedges;
cout<<”\n\t”<<”Enter edges(u,v,weight):”;
for(i=0;i<noofedges;i++)
{
cout<<”\t”;
cin>>u>>v>>w;
G[u][v]=G[v][u]=w;
}
cout<<”\n\t”<<”Enter the starting node:”;
cin>>n;
dijkstra(G,n,u);
}
Void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
vidited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i[<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
prd[i]=nextnode;
}
cout++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
cout<<”\n\n\t”<<”Distance of”<<i<<”=”<<distance[i];
cout<<”\n\n\t”<<”The path is”<<i;
j=i;
do
{
j=pred[j]
cout<<”<-“<<j;
}
while(j!=startnode);
}
}
Input output:-
Dijskatra’s Algorithm
*------------------------*
Enter no.of vertices: 4
No.of edges: 5
Enter edges(u,v,weight): 0 1 2
121
231
303
021
Enter the starting node:1
Distance of 0=2
The path is 0<-1
Distance of 2=1
The path is 2<-1
Distance of 3=3
The Path if 3 <-2<-1
Process returned 0(0X0) execution time:44.616 s
Press any key to continue=3<-2<-1

Dijskatra’s Algorithm
*------------------------*
Enter no.of vertices: 6
No.of edges: 11
Enter edges(u,v,weight): 0 1 50
0 3 10
0 2 45
1 2 10
2 4 30
4 2 35
543
3 4 15
3 0 20
1 3 15
4 1 20
Enter the starting node:0
Distance of 1=31
The path is 1<-2<-3<-0
Distance of 2=21
The path is 2<-3<-0
Distance of 3=20
The Path if 3 <-0
Distance of 4=35
The Path if 4 <-3 <-0
Distance of 5=38
The Path if 5 <-4 <-3 <-0

Process returned 0(0X0) execution time:44.616 s


Press any key to continue=3<-2<-1
BINARY SEARCH USING DIVIDE AND CONQUER TECHNIQUE

#include <iostream>
using namespace std;
int main()
{
int c, first, last, middle, n, search, array[100];
cout<<"\n"<<"\t"<<"\t"<<"BINARY SEARCH";
cout<<"\n"<<"\t"<<"\t"<<"*-------------------------*";
cout<<"\nEnter number of elements :";
cin>>n;
cout<<"Enter "<<n<<" integers in ascending order";
for (c = 0; c < n; c++)
cin>>array[c];
cout<<"\nEnter value to find :";
cin>>search;
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last)
{
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search)
{
cout<<search<<" found at location "<< middle+1;
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
cout<<search<<"Not found! "<<" is not present in the list.";
}
Input Output:-
BINARY SEARCH
*----------------------*
Enter number of elements : 5
Enter 5 integers in ascending order
56
78
99
123
679
Enter value to find : 56
56 found at location 1.
QUICK SORT

#include <iostream>
using namespace std;
void quicksort(int[],int,int);
int main()
{
int x[20],size1,i;

cout<<"\n"<<"\t \t"<<"QUICK SORT";


cout<<"\n"<<"\t \t"<<"*---------*";
cout<<"\n \t"<<"Enter size of the array : ";
cin>>size1;
cout<<"\n \tEnter "<<size1<<" elements: ";
for(i=0;i<size1;i++)
{cin>>x[i];
cout<< "\t ";
}
int s;
s=size1-1;
quicksort(x,0,s);
cout<<"\n Sorted elements using Quick sort: \n \n";
for(i=0;i<size1;i++)
cout<<" "<<x[i];
}

void quicksort(int x[10],int first,int last)


{
int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}
Input Output:-

QUICK SORT
*---------*
Enter size of the array : 10
Enter 10 elements: 26
12
22
11
10
9
34
11
77
75
Sorted elements using Quick sort:
9 10 11 11 12 22 26 34 75 77
Process returned 0 (0x0) execution time : 59.639 s
Press any key to continue.
MERGE SORT

#include <iostream>
using namespace std;

void merge_split(int a[],int first,int last);


void merge(int a[],int f1,int l1,int f2,int l2);
int a[25],b[25];
int main()
{
int i,n;
cout<<"\n"<<"\t \t"<<"MERGE SORT";
cout<<"\n "<<"\t \t"<<"*--------*";
cout<<"\n \n"<<" Enter the limit: ";
cin>>n;
cout<<"\n Enter the elements : ";
for(i=0;i<n;i++)
{
cin>>a[i];
cout<<" ";
}

merge_split(a,0,n-1);
cout<<"\n \n "<<" The sorted list using Merge Sort is : \n \n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
int getc();
}
void merge_split(int a[],int first, int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
merge_split(a,first,mid);
merge_split(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}
void merge(int a[], int f1, int l1, int f2, int l2)
{
int i,j,k=0;
i=f1;
j=f2;
while(i<=l1&&j<=l2)
{
if(a[i]<a[j])
b[k]=a[i++];
else
b[k]=a[j++];
k++;
}
while(i<=l1)
b[k++]=a[i++];
while (j<=l2)
b[k++]=a[j++];
i=f1;
j=0;
while(i<=l2 && j<k)
a[i++]=b[j++];
}
Input Output:-
MERGE SORT
*--------*
Enter the limit: 10
Enter the elements : 99
11
21
13
31
23
45
41
6
67
The sorted list using Merge Sort is :
6 11 13 21 23 31 41 45 67 99
Process returned 0 (0x0) execution time : 39.374 s
Press any key to continue.
KNAPSACK PROBLEM

#include<iostream>
using namespace std;
int main()
{
int array[2][100], n, w, i, curw, used[100], maxi = -1, totalprofit = 0;
cout<<” knapsack problem”;
cout<<”*-----------------------*“;
cout << "Enter number of objects: ";
cin >> n;
cout << "Enter the weight of the knapsack: ";
cin >> w;
for (i = 0; i < n; i++)
{
cin >> array[0][i] >> array[1][i];
}
for (i = 0; i < n; i++)
{
used[i] = 0;
}
curw = w;
while (curw >= 0)
{
maxi = -1;
for (i = 0; i < n; i++)
{
if ((used[i] == 0) && ((maxi == -1) || (((float) array[1][i]/ (float) array[0][i]) > ((float)
array[1][maxi]/ (float) array[0][maxi]))))
{
maxi = i;
}
}
used[maxi] = 1;
curw -= array[0][maxi];
totalprofit += array[1][maxi];
if (curw >= 0)
{
cout << "\nAdded object " << maxi + 1 << " Weight: "
<< array[0][maxi] << " Profit: " << array[1][maxi]
<< " completely in the bag, Space left: " << curw;
}
else
{
cout << "\nAdded object " << maxi + 1 << " Weight: "
<< (array[0][maxi] + curw) << " Profit: "
<< (array[1][maxi] / array[0][maxi]) * (array[0][maxi]
+ curw) << " partially in the bag, Space left: 0"
<< " Weight added is: " << curw + array[0][maxi];
totalprofit -= array[1][maxi];
totalprofit += ((array[1][maxi] / array[0][maxi]) * (array[0][maxi]
+ curw));
}
}
cout << "\nBags filled with objects worth: " << totalprofit;
return 0;
}
Input Output:-
knapsack problem
*---------------------*
Enter number of objects: 3
Enter the weight of the knapsack: 50
10 60
20 100
30 120

Added object 1 Weight: 10 Profit: 60 completely in the bag, Space left: 40


Added object 2 Weight: 20 Profit: 100 completely in the bag, Space left: 20
Added object 3 Weight: 20 Profit: 80 partially in the bag, Space left: 0 Weight added is: 20
Bags filled with objects worth: 240
Process returned 0 (0x0) execution time : 60.081 s
Press any key to continue.
TRAVELING SALESMAN PROBLEM

#include <iostream>
using namespace std;
int matrix[25][25], visited_cities[10], limit, cost = 0;
int tsp(int c)
{
int count, nearest_city = 999;
int minimum = 999, temp;
for(count = 0; count < limit; count++)
{
if((matrix[c][count] != 0) && (visited_cities[count] == 0))
{
if(matrix[c][count] < minimum)
{
minimum = matrix[count][0] + matrix[c][count];
}
temp = matrix[c][count];
nearest_city = count;
}
}
if(minimum != 999)
{
cost = cost + temp;
}
return nearest_city;
}
void minimum_cost(int city)
{
int nearest_city;
visited_cities[city] = 1;
Cout<<city + 1;
nearest_city = tsp(city);
if(nearest_city == 999)
{
nearest_city = 0;
Cout<<nearest_city + 1;
cost = cost + matrix[city][nearest_city];
return;
}
minimum_cost(nearest_city);
}
int main()
{
int i, j;
Cout<<“\n Traveling salesman problem”;
Cout<<“\n*----------------------------------*”;
Cout<<"Enter Total Number of Cities:\t";
cin>>limit;
Cout<<"\nEnter Cost Matrix\n";
for(i = 0; i < limit; i++)
{
Cout<<"\nEnter %d Elements in Row[%d]\n"<< limit, i + 1;
for(j = 0; j < limit; j++)
{
Cin>>matrix[i][j]);
}
visited_cities[i] = 0;
}
Cout<<\nEntered Cost Matrix\n";
for(i = 0; i < limit; i++)
{
Cout<<"\n";
for(j = 0; j < limit; j++)
{
Cout<<matrix[i][j];
}
}
Cout<<"\n\nPath:\t";
minimum_cost(0);
Cout<<"\n\nMinimum Cost: \t";
Cout<<"%d\n"<<cost;
return 0;
}
Input Output:-
Traveling salesman problem
*----------------------------------*
Enter Total Number of Cities: 3
Enter Cost Matrix
Enter 3 Elements in Row[1]
168
Enter 3 Elements in Row[2]
197
Enter 3 Elements in Row[3]
246
Entered Cost Matrix
168
197
246
Path: 1 3 2 1
Minimum Cost: 13
Process returned 0 (0x0) execution time : 33.772 s
Press any key to continue.
8 QUEENS PROBLEM

#define N 8
#include <stdbool.h>
#include <iostream>
using namespace std;
void printSolution(int board[N][N])
{
Cout<<"\n \n \t 8 Queens Problem with the design of Backtracking \n \n";
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cout<<"\t "<<board[i][j];
cout<<"\n\n";
}
}
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}
bool solveNQUtil(int board[N][N], int col)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++)
{
if (isSafe(board, i, col))
{
board[i][col] = 1;
if (solveNQUtil(board, col + 1))
return true;
board[i][col] = 0;
}
}
return false;
}
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 ,0,0,0,0},
{ 0, 0, 0, 0 ,0,0,0,0},
{ 0, 0, 0, 0 ,0,0,0,0},
{ 0, 0, 0, 0 ,0,0,0,0},
{ 0, 0, 0, 0 ,0,0,0,0},
{ 0, 0, 0, 0 ,0,0,0,0},
{ 0, 0, 0, 0 ,0,0,0,0},
{ 0, 0, 0, 0 ,0,0,0,0}};
if (solveNQUtil(board, 0) == false)
{
Cout<<"Solution does not exist";
return false;
}

printSolution(board);
return true;
}
int main()
{
solveNQ();
return 0;
}
Input Output:-
8 Queens Problem with the design of Backtracking
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0

Process returned 0 (0x0) execution time : 0.031 s


Press any key to continue.

You might also like