DS Lab File
DS Lab File
CIE-I
S.N. Date Name of Marks Obtained Remark and
Experiments A(6) C(6) V(5) R(3) Total(20) Signature of
faculty
CIE-II
LT Date Name of Marks Obtained Remark and
Experiment Signature of
A(5) C(5) V(10) R(10) Total(30) faculty
LT-1
LT-2
Marks Obtained
CIE-I CIE-II Total Marks (25) Faculty HoD signature
(Scaled to 10) (Scaled to 15) Signature
Code :
#include <stdio.h>
#include<conio.h>
void main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element at %d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
Output:
8 12 16
9 8 26
12 16 10
3
1 (b) Multiplication of Two 2D arrays:
Code :
#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
Output:
5
Program #2
2- Transpose of a 2D array:
AIM: Write a program in C to find transpose of a 2D array.
Code :
#include <stdio.h>
#include<conio.h>
void main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");
for (c = 0; c < m; c++)
for(d = 0; d < n; d++)
scanf("%d", &matrix[c][d]);
for (c = 0; c < m; c++)
for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
printf("Transpose of the matrix:\n");
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
printf("%d\t", transpose[c][d]);
printf("\n");
}
getch();
}
Output:
Original Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Transposed Matrix:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
6
Program #3
3- Implementation of stack using array:
Code:
#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
void main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t Stack Operations ");
printf("\n\t--------------------------------");
printf("\n\t [Link]\n\t [Link]\n\t [Link]\n\t [Link]");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
7
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
getch();
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
8
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
Output:
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 10
Pushed item: 10
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 20
Pushed item: 20
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 3
Top item of the stack: 20
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
9
Enter your choice: 4
Stack contents:
20
10
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 2
Popped item: 20
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack contents:
10
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 5
Exiting...
10
Program #4
4- Implementation of queue using array:
Code:
#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\[Link] \[Link] \[Link] \[Link]");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
11
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}
Output:
Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to enqueue: 10
Enqueued: 10
Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to enqueue: 20
Enqueued: 20
Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
12
5. Exit
Enter your choice: 4
Queue: 10 20
Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 3
Peeked item: 10
Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 2
Dequeued: 10
Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 4
Queue: 20
Select an operation:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 5
Exiting...
13
Program #5
5- Implementation of circular queue using array:
Code:
#include <stdio.h>
#include<conio.h>
#define size 5
int front = - 1;
int rear = - 1;
void main()
{
int n, ch;
int queue[size];
do
{
printf("\n\n Circular Queue:\n1. Insert \n2. Delete\[Link]\n0. Exit");
printf("\nEnter Choice 0-3? : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter number: ");
scanf("%d", &n);
insertq(queue, n);
break;
case 2:
deleteq(queue);
break;
case 3:
display(queue);
break;
}
}while (ch != 0);
}
Output:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 10
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
10 20 30
1. Enqueue
2. Dequeue
3. Display
16
4. Exit
Enter your choice: 2
Dequeued item: 10
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
20 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...
17
Program #6
6- Implementation of a stack using linked list.
Code:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node *next;
}*top;
void display()
18
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
Output:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 10
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 20
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the item to push: 30
20
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Current stack:
30 -> 20 -> 10 -> None
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 2
Popped item: 30
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 3
Top item: 20
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 5
Exiting...
21
Program #7
7- Implementation of a queue using linked list.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
22
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
void main()
{
int i=0;
front=NULL;
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
}
case 3:
23
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
Output:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 10
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
10 20 30
24
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Dequeued item: 10
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
20 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...
25
Program #8
8- Implementation of a circular queue using linked list.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=rear;
}
}
void display()
{
26
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=front)
{
printf("\t%d",var->Data);
var=var->next;
}
if(var==front)
{
printf("\t%d",var->Data);
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
Output:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 10
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the item to enqueue: 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
28
10 20 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Dequeued item: 10
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Current queue:
20 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...
29
Program #9
9- Implementation of a binary tree using linked list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the binary tree
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
int main() {
struct Node* root = NULL;
int choice, data;
while (1) {
printf("\n1. Insert Node\n");
printf("2. Inorder Traversal\n");
printf("3. Preorder Traversal\n");
printf("4. Postorder Traversal\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
31
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}
Output:
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 50
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 30
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 70
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 2
Preorder Traversal: 50 30 70
1. Insert Node
2. Inorder Traversal
32
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 3
Inorder Traversal: 30 50 70
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 4
Postorder Traversal: 30 70 50
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 5
Exiting...
33
Program #10
10- Implementation of a binary search tree using linked list.
AIM: Write a program in C to implement a binary search tree using linked list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the binary search tree
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int main() {
struct Node* root = NULL;
int choice, data;
while (1) {
printf("\n1. Insert Node\n");
printf("2. Inorder Traversal\n");
printf("3. Preorder Traversal\n");
printf("4. Postorder Traversal\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorder(root);
35
printf("\n");
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}
Output:
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 50
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 30
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 1
Enter data to insert: 70
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 2
Preorder Traversal: 50 30 70
1. Insert Node
36
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 3
Inorder Traversal: 30 50 70
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 4
Postorder Traversal: 30 70 50
1. Insert Node
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter your choice: 5
Exiting...
37
Program #11
11- Implementation of a tree traversal using linked list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the binary tree
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int main() {
struct Node* root = NULL;
int choice, data;
// Inserting nodes into the binary tree
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
while (1) {
printf("\n1. Inorder Traversal\n");
printf("2. Preorder Traversal\n");
printf("3. Postorder Traversal\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 2:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 3:
39
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}
Output:
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
4. Exit
Enter your choice: 1
Inorder Traversal: 20 30 40 50 60 70 80
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
4. Exit
Enter your choice: 2
Preorder Traversal: 50 30 20 40 70 60 80
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
4. Exit
Enter your choice: 3
Postorder Traversal: 20 40 30 60 80 70 50
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
4. Exit
Enter your choice: 4
Exiting...
40
Program #12
12- Implementation of BFS using linked list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the adjacency list
struct AdjListNode {
int dest;
struct AdjListNode* next;
};
// Define a structure for the adjacency list
struct AdjList {
struct AdjListNode* head;
};
// Define a structure for the graph
struct Graph {
int V;
struct AdjList* array;
};
// Function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode = (struct AdjListNode*)malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// Function to create a graph with V vertices
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->array = (struct AdjList*)malloc(V * sizeof(struct AdjList));
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}
// Function to add an edge to the graph
void addEdge(struct Graph* graph, int src, int dest) {
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
}
// Function to perform breadth-first search traversal
41
void BFS(struct Graph* graph, int start) {
// Create a queue for BFS
int* queue = (int*)malloc(graph->V * sizeof(int));
int front = 0, rear = 0;
// Mark all the vertices as not visited
int* visited = (int*)malloc(graph->V * sizeof(int));
for (int i = 0; i < graph->V; ++i) {
visited[i] = 0;
}
int main() {
int V, E, start;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &V);
struct Graph* graph = createGraph(V);
printf("Enter the number of edges in the graph: ");
scanf("%d", &E);
printf("Enter the edges (src dest):\n");
for (int i = 0; i < E; ++i) {
int src, dest;
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
}
printf("Enter the starting vertex for BFS traversal: ");
42
scanf("%d", &start);
printf("BFS Traversal: ");
BFS(graph, start);
printf("\n");
return 0;
}
Output:
43
Program #13
13- Implementation of DFS using linked list.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int V, E, i, src, dest;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &V);
printf("Enter the number of edges in the graph: ");
scanf("%d", &E);
struct Graph* graph = createGraph(V);
printf("Enter the edges (src dest):\n");
for (i = 0; i < E; ++i) {
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
}
int start;
printf("Enter the starting vertex for DFS traversal: ");
scanf("%d", &start);
printf("Depth-First Search (DFS) traversal starting from vertex %d: ", start);
DFS(graph, start);
printf("\n");
45
return 0;
}
Output:
46
Program #14
14- Implementation of linear search.
Code:
#include <stdio.h>
int main() {
int n, key;
// Input the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Input the elements of the array
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the element to be searched
printf("Enter the element to search: ");
scanf("%d", &key);
// Perform linear search
int index = linearSearch(arr, n, key);
// Display the result
if (index != -1) {
printf("Element %d found at index %d.\n", key, index);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}
Output:
48
Program #15
15- Implementation of binary search.
Code:
#include <stdio.h>
// Function to perform binary search
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1; // Element not found
}
int main() {
int size, key, result;
// Input size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
// Input elements of the array
int arr[size];
printf("Enter the elements of the array in sorted order:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Input the element to search for
printf("Enter the element to search for: ");
scanf("%d", &key);
// Perform binary search
result = binarySearch(arr, size, key);
// Display result
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
49
}
Output:
50
Program #16
16- Implementation of the bubble sort.
Code:
#include <stdio.h>
// Function to perform bubble sort
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int n, i;
// Input the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Input the elements of the array
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Sort the array using bubble sort
bubbleSort(arr, n);
// Display the sorted array
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
51
Enter the number of elements in the array: 5
Enter 5 elements:
12 5 8 19 3
Sorted array: 3 5 8 12 19
52
Program #17
17- Implementation of selection sort.
Code:
#include <stdio.h>
// Function to perform selection sort
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the minimum element with the current element
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Perform selection sort
selectionSort(arr, n);
// Display the sorted array
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
53
Enter the number of elements: 5
Enter the elements:
12
45
7
23
9
Sorted array:
7 9 12 23 45
54
Program #18
18- Implementation of insertion sort.
Code:
#include <stdio.h>
// Function to perform insertion sort
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current
position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Perform insertion sort
insertionSort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
56
Program #19
19- Implementation of merge sort.
Code:
#include <stdio.h>
#include <stdlib.h>
// Merge two subarrays of arr[]
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
// Create temporary arrays
int L[n1], R[n2];
// Copy data to temporary arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
// Merge the temporary arrays back into arr[l..r]
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
Output:
59
Program #20
20- Implementation of heap sort.
Code:
#include <stdio.h>
#include <stdlib.h>
// Function to heapify a subtree rooted with node i which is an index in arr[]
void heapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
int main() {
60
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Perform heap sort
heapSort(arr, n);
// Print the sorted array
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
61