0% found this document useful (0 votes)
2 views

Program and Output

The document provides implementations of various Abstract Data Types (ADTs) including List, Stack, and Queue using both arrays and linked lists in C++. It includes functions for creating, inserting, deleting, displaying, and searching elements in these data structures. Additionally, it covers infix to postfix conversion using a stack and operations for inserting and deleting elements in a doubly linked list.

Uploaded by

smahash645
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program and Output

The document provides implementations of various Abstract Data Types (ADTs) including List, Stack, and Queue using both arrays and linked lists in C++. It includes functions for creating, inserting, deleting, displaying, and searching elements in these data structures. Additionally, it covers infix to postfix conversion using a stack and operations for inserting and deleting elements in a doubly linked list.

Uploaded by

smahash645
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

1.A.

LIST ADT USING ARRAY:

#include<iostream.h>
#include<conio.h>
#define maxsize 10
int list[maxsize],n;
void Create();
void Insert();
void Delete();
void Display();
void Search();
void main()
{
int choice;
clrscr();
do
{
cout<<"Array Implementation of List"<<endl;
cout<<"1.create"<<endl;
cout<<"2.Insert"<<endl;
cout<<"3.Delete"<<endl;
cout<<"4.Display"<<endl;
cout<<"5.Search"<<endl;
cout<<"Enter your choice:"<<endl;
cin>>choice;
switch(choice)
{
case 1: Create();
break;
case 2: Insert();
break;
case 3: Delete();
break;
case 4: Display();
break;
case 5: Search();
break;
default: cout<<"Enter option between 1 - 5"<<endl;
break;
}
}
while(choice<6);
}
void Create()
{
int i;
cout<<"Enter the number of elements to be added in the list:"<<endl;
cin>>n;
cout<<"Enter the array elements:"<<endl;
for(i=0;i<n;i++)
cin>>list[i];
Display();
}
void Insert()
{
int i,data,pos;
cout<<"Enter the data to be inserted:"<<endl;
cin>>data;
cout<<"Enter the position at which element to be inserted:"<<endl;
cin>>pos;
for(i = n-1 ; i >= pos-1 ; i--)
list[i+1] = list[i];
list[pos-1] = data;
n+=1;
Display();
}
void Delete( )
{
int i,pos;
cout<<"Enter the position of the data to be deleted:"<<endl;
cin>>pos;
cout<<"The data deleted is:"<<list[pos-1]<<endl;
for(i=pos-1;i<n-1;i++)
list[i]=list[i+1];
n=n-1;
Display();
}
void Display()
{
int i;
cout<<"********** Elements in the array**********"<<endl;
for(i=0;i<n;i++)
cout<<" "<<list[i]<<endl;
}
void Search()
{
int search,i,count = 0;
cout<<"Enter the element to be searched:"<<endl;
cin>>search;
for(i=0;i<n;i++)
{
if(search == list[i])
{
count++;
}
}
if(count==0)
cout<<"Element not present in the list"<<endl;
else
cout<<"Element present in the list"<<endl;
getch();
}
Output:

Array Implementation of List


1.create
2.Insert
3.Delete
4.Display
5.Search
Enter your choice:
1
Enter the number of elements to be added in the list: 5
Enter the array elements:
12345
*********Elements in the array*********
1
2
3
4
5
Array Implementation of List
1.create
2.Insert
3.Delete
4.Display
5.Search
Enter your choice:
2
Enter the data to be inserted: 3
Enter the position at which element to be inserted:1
*********Elements in the array*********
3
1
2
3
4
5
Array Implementation of List
1.create
2.Insert
3.Delete
4.Display
5.Search
Enter your choice:
3
Enter the position of the data to be deleted: 4
The data deleted is: 3
*********Elements in the array*********
3
1
2
4
5
Array Implementation of List
1.create
2.Insert
3.Delete
4.Display
5.Search
Enter your choice:
5
Enter the element to be searched: 1
Element present in the list
Array Implementation of List
1.create
2.Insert
3.Delete
4.Display
5.Search
Enter your choice:6
1.B. LIST ADT USING LINKED LIST:

#include<iostream.h>
#include<conio.h>
struct Node
{
int num;
Node* next;
};
struct Node* head = NULL;
void insertNode(int n)
{
struct Node* newNode = new Node;
newNode->num = n;
newNode->next = head;
head = newNode;
}
void display()
{
if (head == NULL)
{
cout << "List is empty!" << endl;
return;
}
struct Node* temp = head;
while (temp != NULL)
{
cout << temp->num << " ";
temp = temp->next;
}
cout << endl;
}
void deleteItem()
{
if (head == NULL)
{
cout << "List is empty!" << endl;
return;
}
cout << head->num << " is removed." << endl;
head = head->next;
}
int main()
{
clrscr();
void display();
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
void display();
deleteItem();
deleteItem();
deleteItem();
deleteItem();
deleteItem();
deleteItem();
void display();
getch();
return 0;
}
Output:

50 is removed!
40 is removed!
30 is removed!
20 is removed!
10 is removed!
2.A.STACK ADT USING LINKED LIST:

#include<iostream.h>
#include<conio.h>
class Node
{
public:
int data;
Node* next;
};
class Stack
{
private:
Node* top;
public:
Stack()
{
top = NULL;
}
void push(int x)
{
Node* newNode = new Node();
newNode->data = x;
newNode->next = top;
top = newNode;
}
void pop()
{
if (top == NULL)
{
cout << "Stack is empty!" << endl;
return;
}
Node* temp = top;
top = top->next;
delete temp;
}
void display()
{
if (top == NULL)
{
cout << "Stack is empty" << endl;
return;
}
Node* temp = top;
cout << "Stack elements are: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
void main()
{
clrscr();
Stack stk;
cout << "Input some elements onto the stack (using linked list):\n";
stk.push(6);
stk.push(5);
stk.push(3);
stk.push(1);
stk.display();
cout << "\nRemove 2 elements from the stack:\n";
stk.pop();
stk.pop();
stk.display();
cout << "\nInput 2 more elements:\n";
stk.push(8);
stk.push(9);
stk.display();
getch();
}
OUTPUT:

Input some elements onto the stack(using linked list)


Stack elements are:1356
Remove 2 elements from the stack:
Stack elements are :56
Input 2 more elements:
Stack elements are:9856
2 .B.STACK ADT USING ARRAY:

#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";
}
void main()
{
clrscr();
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);
getch();
}
OUTPUT:

1.push in stack
2.pop from stack
3.display stack
4.exit
Enter choice:
1
Value to pushed: 7
Enter choice:
3
Stack elements are:7
Enter choice:
4
Exit
3 .A.QUEUE ADT USING ARRAY:

#include <iostream.h>
#include <conio.h>
int queue[100], n = 100, front = -1, rear = -1;
void Insert()
{
int val;
if (rear == n - 1)
cout << "Queue Overflow" << endl;
else
{
if (front == -1)
front = 0;
cout << "Insert the element in queue : " << endl;
cin >> val;
rear++;
queue[rear] = val;
}
}
void Delete()
{
if (front == -1 || front > rear)
{
cout << "Queue Underflow ";
return;
}
else
{
cout << "Element deleted from queue is : " << queue[front] << endl;
front++;
}
}
void Display()
{
if (front == -1)
cout << "Queue is empty" << endl;
else
{
cout << "Queue elements are : ";
for (int i = front; i <= rear; i++)
cout << queue[i] << " ";
cout << endl;
}
}
int main()
{
clrscr();
int ch;
cout << "1) Insert element to queue" << endl;
cout << "2) Delete element from queue" << endl;
cout << "3) Display all the elements of queue" << endl;
cout << "4) Exit" << endl;
do
{
cout << "Enter your choice : " << endl;
cin >> ch;
switch (ch)
{
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout << "Exit" << endl;
break;
default: cout << "Invalid choice" << endl;
}
}
while (ch != 4);
getch();
return 0;
}
Output :

1.Insert element to queue


2.Delete element from queue
3.Display all the elements of queue
4.Exit
Enter your choice : 1
Insert the element in queue:4
Enter your choice:1
Insert the element in queue : 3
Enter your choice : 1
Insert the element in queue : 5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
Queue elements are : 3 5
Enter your choice : 7
Invalid choice
Enter your choice : 4
Exit
3. B.QUEUE ADT USING LINKED LIST:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert()
{
int val;
cout << "Insert the element in queue : " << endl;
cin >> val;
if (rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
}
else
{
temp = (struct node *)malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
void Delete()
{
temp = front;
if (front == NULL)
{
cout << "Underflow" << endl;
return;
}
else if (temp->next != NULL)
{
temp = temp->next;
cout << "Element deleted from queue is : " << front->data << endl;
free(front);
front = temp;
}
else
{
cout << "Element deleted from queue is : " << front->data << endl;
free(front);
front = NULL;
rear = NULL;
}
}
Void Display()
{
temp = front;
if ((front == NULL) && (rear == NULL))
{
cout << "Queue is empty" << endl;
return;
}
cout << "Queue elements are: ";
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void main()
{
int ch;
clrscr();
cout << "1) Insert element to queue" << endl;
cout << "2) Delete element from queue" << endl;
cout << "3) Display all the elements of queue" << endl;
cout << "4) Exit" << endl;
do {
cout << "Enter your choice : ";
cin >> ch;
switch (ch)
{
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout << "Exit" << endl;
break;
default: cout << "Invalid choice" << endl;
}
}
while (ch != 4);
getch();
}
Output :

1.Insert element to queue


2.Delete element from queue
3.Display all the elements of queue
4.Exit
Enter your choice : 1
Insert the element in queue : 4
Enter your choice : 1
Insert the element in queue : 3
Enter your choice : 1
Insert the element in queue : 5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
Queue elements are : 3 5
Enter your choice : 7
Invalid choice
Enter your choice : 4
Exit
4.INFIX TO POSTFIX CONVERSION USING STACK:

#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
const int MAX = 50;
Class infix
{
Private:
Char target [MAX], stack[MAX];
Char*s, *t;
int top;
Public:
infix();
Void setexpr(char *str);
Void push(char c);
char pop();
Void convert();
int priority(char c);
Void show();
};
infix : : infix()
{
top=-1;
strcpy(target,” ”);
strcpy(stack,” ”);
t=target;
s=” ”;
}
Void infix :: setexpr(char*str)
{
s=str;
}
Void infix::push(char c)
{
if(top==MAX)
Cout<<”\nstack is full\n”;
else
{
top++;
Stack[top]=c;
}
}
Char infix :: pop()
{
if(top==-1)
{
cout<<”\nstack is empty\n”;
return -1;
}
else
{
Char item=stack[top];
top--;
return item;
}
}
Void infix::convert()
{
while(*s)
{
if(*s==’ ’||*s==’\t’)
{
s++;
continue;
}
if(isdigit(*s)||isalpha(*s))
{
while(isdigit(*s)||isalpha(*s))
{
*t=*s;
s++;
t++;
}
}
if(*s==’(‘)
{
Push(*s);
s++;
}
char opr;
if(*s==’*’||*s==’+’||*s==’\’||*s==’%’||*s==’-’||*s==’$’)
{
if(top!=-1)
{
opr=pop();
while(priority(opr)>=priority(*s))
{
*t=opr;
t++;
opr=pop();
}
Push(opr);
Push(*s);
}
else
Push(*s);
s++;
}
if(*s==’)’)
{
opr=pop();
While((opr)!=’(‘)
{
*t=opr;
t++;
opr=pop();
}
s++;
}
}
while(top!=-1)
{
char opr=pop();
*t=opr;
t++;
}
*t=’/0’;
}
int infix::priority(char c)
{
if(c==’$’)
return 3;
if(c==’*’||c==’/’||c==’%’)
return 2;
else
{
if(c==’+’||c==’-’)
return 1;
else
return 0;
}
}
Void infix::show()
{
cout<<target;
}
Void main()
{
clrscr();
char expr[MAX];
infix q;
cout<<”\n enter an expression in infix form:”;
cin.getline(expr,MAX);
q.setexpr(expr);
q.convert();
cout<<”\n the postfix expression is:”;
q.show();
getch();
}
Output:

Enter an expression in infix form:(a+b)*(c-d)


The postfix expression is:ab+cd-*
5.A&B.INSERT AND DELETE AN ELEMENT IN DOUBLY LINKED LIST:

#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node *prev, *next;
};
void deleteNode(Node** head_ref, Node* del)
{
if (*head_ref == NULL || del == NULL)
{
return;
}
if (*head_ref == del)
{
*head_ref = del->next;
}
if (del->next != NULL)
{
del->next->prev = del->prev;
}
if (del->prev != NULL)
{
del->prev->next = del->next;
}
delete del;
return;
}
void insertNode(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
{
(*head_ref)->prev = new_node;
}
(*head_ref) = new_node;
}
void printLinkedList(Node* node)
{
while (node != NULL)
{
cout << node->data << " -> ";
node = node->next;
}
}
int main()
{
clrscr();
Node* head = NULL;
insertNode(&head, 1);
insertNode(&head, 2);
insertNode(&head, 3);
insertNode(&head, 4);
insertNode(&head, 5);
cout << "Doubly linked list insertion list is:\n" << endl;
printLinkedList(head);
deleteNode(&head, head->next);
cout << "\n Doubly linked list deletion list are:\n" << endl;
printLinkedList(head);
deleteNode(&head, head->next);
cout << "\n Doubly linked list deletion list are:\n" << endl;
printLinkedList(head);
getch();
return 0;
}
Output:

Doubly linked list insertion list is: 5 ->4 ->3->2->1->


Doubly linked list deletion list are: 5->3->2->1->
Doubly linked list deletion list are: 5->2->1->
5.C.SEARCH AN ELEMENT IN DOUBLY LINKED LIST:

#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node* next;
};
class LinkedList
{
private:
Node* head;
public:
LinkedList()
{
head = NULL;
}
void push_back(int newElement)
{
Node* newNode = new Node();
newNode->data = newElement;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
}
else
{
Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
void SearchElement(int searchValue)
{
Node* temp = head;
int found = 0;
int i = 0;
if (temp != NULL)
{
while (temp != NULL)
{
i++;
if (temp->data == searchValue)
{
found++;
break;
}
temp = temp->next;
}
if (found == 1)
{
cout << searchValue << " is found at index = " << i << ".\n";
}
else
{
cout << searchValue << " is not found in the list.\n";
}
}
else
{
cout << "The list is empty.\n";
}
}
void PrintList()
{
Node* temp = head;
if (temp != NULL)
{
cout << "The list contains: ";
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
else
{
cout << "The list is empty.\n";
}
}
};
void main()
{
clrscr();
LinkedList MyList;
MyList.push_back(10);
MyList.push_back(20);
MyList.push_back(30);
MyList.PrintList();
MyList.SearchElement(10);
MyList.SearchElement(15);
MyList.SearchElement(20);
getch();
}
Output:

The list contains:10 20 30


10 is found at index=1.
15 is not found at index.
20 is found at index=2.
6.A&B.INSERT AND DELETE AN ELEMENT IN BINARY SEARCH TREE:

#include<iostream.h>
#include<conio.h>
struct Node
{
int key;
Node *left, *right;
Node(int value) : key(value), left(NULL), right(NULL) {}
};
Node* insertnode(Node* root, int key)
{
if (root == NULL) return new Node(key);
if (key < root->key)
root->left = insertnode(root->left, key);
else if (key > root->key)
root->right = insertnode(root->right, key);
return root;
}
Node* minValueNode(Node* root)
{
Node* current = root;
while (current && current->left != NULL) current = current->left;
return current;
}
Node* deleteNode(Node* root, int key)
{
if (root == NULL) return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else
{
if (root->left == NULL)
{
Node* temp = root->right;
delete root;
return temp;
}
else if (root->right == NULL)
{
Node* temp = root->left;
delete root;
return temp;
}
Node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
return root;
}
void inorder(Node* root)
{
if (root != NULL)
{
inorder(root->left);
cout << root->key << "-> ";
inorder(root->right);
}
}
void main()
{
clrscr();
Node* root = NULL;
root = insertnode(root, 50);
insertnode(root, 30);
insertnode(root, 20);
insertnode(root, 40);
insertnode(root, 70);
insertnode(root, 60);
insertnode(root, 80);
cout << "BST insertion ( before deletion ): ";
inorder(root);
cout << endl;
int keyToDelete = 30;
root = deleteNode(root, keyToDelete);
cout << "BST deletion ( after deleting " << keyToDelete << " ) : ";
inorder(root);
cout << endl;
getch();
}
OUTPUT:

BST insertion ( befor deletion) :20->30->40->50->60->70->80->


BST deletion ( after deleting 30) :20->40->50->60->70->80->
6.C . INORDER, PREORDER AND POSTORDER TRAVERSAL:

#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node* left;
Node* right;
Node(int value)
{
data = value;
left = right = NULL;
}
};
void inorder(Node* root)
{
if (root != NULL)
{
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
void preorder(Node* root)
{
if (root != NULL) {
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node* root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
}
void main()
{
clrscr();
Node* root = new Node(10);
root->left = new Node(5);
root->right = new Node(15);
root->left->left = new Node(3);
root->left->right = new Node(7);
root->right->left = new Node(12);
root->right->right = new Node(18);
cout << "Inorder traversal: ";
inorder(root);
cout << endl;
cout << "Preorder traversal: ";
preorder(root);
cout << endl;
cout << "Postorder traversal: ";
postorder(root);
cout << endl;
getch();}
OUTPUT:

Inorder traversal:3 5 7 10 12 15 18
Preorder traversal:10 5 3 7 15 12 18
Postorder traversal: 3 7 5 12 18 15 10
7.IMPLEMENTATION OF BFS AND DFS FOR A GIVEN GRAPH:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
const int MAX = 100;
class Graph
{
int vertices;
int adjList[MAX][MAX];
public:
Graph(int v) : vertices(v)
{
for (int i = 0; i < MAX; i++)
for (int j = 0; j < MAX; j++)
adjList[i][j] = 0;
}
void addedge(int u, int v)
{
adjList[u][v] = 1;
adjList[v][u] = 1;
}
void bfs(int start)
{
int visited[MAX] = {0};
int queue[MAX];
int front = 0, rear = -1;
visited[start] = 1;
queue[++rear] = start;
while (front <= rear)
{
int current = queue[front++];
cout << current << " ";
for (int neighbor = 0; neighbor < vertices; neighbor++)
{
if (adjList[current][neighbor] && !visited[neighbor])
{
visited[neighbor] = 1;
queue[++rear] = neighbor;
}
}
}
}
void dfs(int start)
{
int visited[MAX] = {0};
int stack[MAX];
int top = -1;
visited[start] = 1;
stack[++top] = start;
while (top != -1)
{
int current = stack[top--];
cout << current << " ";
for (int neighbor = 0; neighbor < vertices; neighbor++)
{
if (adjList[current][neighbor] && !visited[neighbor])
{
visited[neighbor] = 1;
stack[++top] = neighbor;
}
}
}
}
};
void main()
{
clrscr();
Graph g(15);
g.addedge(0, 1);
g.addedge(0, 2);
g.addedge(1, 3);
g.addedge(1, 4);
g.addedge(2, 5);
cout << "BFS starting from vertex 0: ";
g.bfs(0);
cout << "\nDFS starting from vertex 0: ";
g.dfs(0);
getch();
}
OUTPUT:

BFS starting from vertex 0: 0 1 2 3 4 5


DFS starting from vertex 0:0 2 5 1 4 3
8.A&B.SEARCH AN ELEMENT IN LINEAR SEARCH AND BINARY SEARCH:

#include <iostream.h>
#include <conio.h>
int linearSearch(int arr[], int size, int target)
{
for (int i = 0; i < size; ++i)
{
if (arr[i] == target)
{
return i;
}
}
return -1;
}
int binarySearch(int arr[], int size, int target)
{
int low = 0;
int high = size - 1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[mid] == target)
{
return mid;
}
else if (arr[mid] < target)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
void main()
{
clrscr();
const int maxSize = 100;
int array[maxSize];
int size = 0;
cout << "Enter the number of elements: ";
cin >> size;
cout << "Enter the array elements (sorted): ";
for (int i = 0; i < size; ++i)
{
cin >> array[i];
}
int target;
cout << "Enter the element to searched in Linear Search: ";
cin >> target;
int linearResult = linearSearch(array, size, target);
if (linearResult != -1)
{
cout << "Linear Search: Element found at index "<< linearResult<< endl;
}
else
{
cout << "Linear Search: Element not found" << endl;
}
cout << "Enter the element to search Binary Search: ";
cin >> target;
int binaryResult = binarySearch(array, size, target);
if (binaryResult != -1)
{
cout << "Binary Search: Element found at index “<<binaryResult<<endl;
}
else
{
cout << "Binary Search: Element not found" << endl;
}
getch();
}
OUTPUT:

Enter the number of elements : 5


Enter array elements (sorted): 1 2 3 4 5
Enter the element to searched in Linear search: 3
Linear search: element found at index:2
Enter the elements to search in Binary search:4
Binary search: element found at index: 3
9.A .BUBBLE SORT:

#include<iostream.h>
#include<conio.h>
#define MAX 100
void swapping(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size)
{
for (int i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
void bubblesort(int *array, int size)
{
for (int i = 0; i < size; i++)
{
int swaps = 0;
for (int j = 0; j < size - 1; j++)
{
if (array[j] > array[j + 1])
{
swapping(array[j], array[j + 1]);
swaps = 1;
}
}
if (!swaps)
break;
}
}
int main()
{
clrscr();
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[MAX];
cout << "Enter elements:" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "Array before sorting: ";
display(arr, n);
bubblesort(arr, n);
cout << "Array after sorting: ";
display(arr, n);
getch();
return 0;
}
OUTPUT:

Enter the number of elements: 5


Enter elements:
58 98 78 12 30 51
Array before sorting:58 98 78 12 30 51
Array after sorting: 12 30 51 58 78 98
9.B.SELECTION SORT:

#include<iostream.h>
#include<conio.h>
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void selectionSort(int arr[], int n)
{
for(int i=0;i< n-;i++)
{
int minIndex = i;
for (int j=i+1;j< n;j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}
void display(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void main()
{
clrscr();
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[50];
cout << "Enter the elements of the array:\n ";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "Before selection sorting: ";
display(arr, n);
selectionSort(arr, n);
cout << "After Selection Sorted: ";
display(arr, n);
getch();
}
OUTPUT:

Enter the number of elements in the array: 6


Enter the elements of the array: 33 46 88 1 22 19
Before selection sorting:33 46 88 1 22 19
After selection sorted:1 19 22 33 46 88
9.C.INSERTION SORT:

#include<iostream.h>
#include<conio.h>
void insertionSort(int arr[], int n)
{
for (int i = 1; i < n; i++)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void display(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int n;
clrscr();
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[50];
cout << "Enter the elements of the array: \n";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "Before insertion sorting: ";
display(arr, n);
insertionSort(arr, n);
cout << "After Insertion Sorted: ";
display(arr, n);
getch();
return 0;
}
Output:

Enter the number of elements in the array:6


Enter the array elements:9 77 5 44 28 69
Before insertion sorting:9 77 5 44 28 69
After insertion sorted:5 9 28 44 69 77

You might also like