Indira Gandhi University, Meerpur
Data Structures & Algorithms
using C
Submitted by: Submitted to:
Name – Sagar Prof. Satinder Bal Gupta
Roll No. – 230011015010 (Head of Dept. CSE)
Class – B.Tech. 3rd Sem.
PROGRAM -1
Write a menu driven program that implements following opera ons (using
separate func ons) on a linear array:
Insert a new element at end as well as at a given posi on
Delete an element from a given whose value is given or whose posi on is
given
To find the loca on of a given element
To display the elements of the linear array
Code :
#include <stdio.h>
#define MAX 100
int array[MAX] = {10, 20, 30, 40, 50};
int size = 5;
void insertAtEnd(int element) {
if (size < MAX) {
array[size++] = element;
prin ("Element inserted at the end.\n");
} else {
prin ("Array is full.\n");
void insertAtPosi on(int element, int posi on) {
if (posi on >= 0 && posi on <= size && size < MAX) {
for (int i = size; i > posi on; i--) {
array[i] = array[i - 1];
array[posi on] = element;
size++;
prin ("Element inserted at posi on %d.\n", posi on);
} else {
prin ("Invalid posi on or array is full.\n");
}
void deleteByValue(int element) {
int found = 0;
for (int i = 0; i < size; i++) {
if (array[i] == element) {
found = 1;
for (int j = i; j < size - 1; j++) {
array[j] = array[j + 1];
size--;
prin ("Element %d deleted.\n", element);
break;
if (!found) {
prin ("Element not found.\n");
void deleteByPosi on(int posi on) {
if (posi on >= 0 && posi on < size) {
for (int i = posi on; i < size - 1; i++) {
array[i] = array[i + 1];
size--;
prin ("Element at posi on %d deleted.\n", posi on);
} else {
prin ("Invalid posi on.\n");
void findElement(int element) {
int found = 0;
for (int i = 0; i < size; i++) {
if (array[i] == element) {
prin ("Element %d found at posi on %d.\n", element, i);
found = 1;
break;
if (!found) {
prin ("Element not found.\n");
void display() {
if (size == 0) {
prin ("Array is empty.\n");
} else {
prin ("Array elements: ");
for (int i = 0; i < size; i++) {
prin ("%d ", array[i]);
prin ("\n");
int main() {
int choice, element, posi on;
do {
prin ("\nMenu:\n");
prin ("1. Insert at end\n");
prin ("2. Insert at posi on\n");
prin ("3. Delete by value\n");
prin ("4. Delete by posi on\n");
prin ("5. Find element\n");
prin ("6. Display array\n");
prin ("7. Exit\n");
prin ("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
prin ("Enter element to insert at end: ");
scanf("%d", &element);
insertAtEnd(element);
display();
break;
case 2:
prin ("Enter element and posi on to insert: ");
scanf("%d%d", &element, &posi on);
insertAtPosi on(element, posi on);
display();
break;
case 3:
prin ("Enter element to delete: ");
scanf("%d", &element);
deleteByValue(element);
display();
break;
case 4:
prin ("Enter posi on to delete element: ");
scanf("%d", &posi on);
deleteByPosi on(posi on);
display();
break;
case 5:
prin ("Enter element to find: ");
scanf("%d", &element);
findElement(element);
display();
break;
case 6:
display();
break;
case 7:
prin ("Exi ng...\n");
break;
default:
prin ("Invalid choice.\n");
} while (choice != 7);
return 0;
Output :
Insert a new element at end as well as at a given posi on
Delete an element from a given whose value is given or whose posi on is
given
To find the loca on of a given element
To display the elements of the linear array
PROGRAM -2
Write a menu driven program that maintains a linear linked list whose
elements are stored in on ascending order and implements the following
opera ons (using separate func ons):
Insert a new element
Delete an exis ng element
Search an element
Display all the elements
Code :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insert(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (*head == NULL || (*head)->data >= value) {
newNode->next = *head;
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL && current->next->data < value) {
current = current->next;
newNode->next = current->next;
current->next = newNode;
prin ("Element %d inserted.\n", value);
}
void delete(struct Node** head, int value) {
struct Node* temp = *head;
struct Node* prev = NULL;
if (temp != NULL && temp->data == value) {
*head = temp->next;
free(temp);
prin ("Element %d deleted.\n", value);
return;
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
if (temp == NULL) {
prin ("Element %d not found.\n", value);
return;
prev->next = temp->next;
free(temp);
prin ("Element %d deleted.\n", value);
void search(struct Node* head, int value) {
struct Node* current = head;
int posi on = 0;
int found = 0;
while (current != NULL) {
if (current->data == value) {
prin ("Element %d found at posi on %d.\n", value, posi on);
found = 1;
break;
current = current->next;
posi on++;
if (!found) {
prin ("Element %d not found.\n", value);
void display(struct Node* head) {
struct Node* current = head;
if (current == NULL) {
prin ("List is empty.\n");
return;
prin ("List elements in ascending order: ");
while (current != NULL) {
prin ("%d ", current->data);
current = current->next;
prin ("\n");
void ini alizeList(struct Node** head) {
int sampleData[] = {10, 20, 30, 40, 50};
int size = sizeof(sampleData) / sizeof(sampleData[0]);
for (int i = 0; i < size; i++) {
insert(head, sampleData[i]);
int main() {
struct Node* head = NULL;
int choice, value;
ini alizeList(&head);
prin ("Ini al list: ");
display(head);
do {
prin ("\nMenu:\n");
prin ("1. Insert element\n");
prin ("2. Delete element\n");
prin ("3. Search element\n");
prin ("4. Display all elements\n");
prin ("5. Exit\n");
prin ("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
prin ("Enter value to insert: ");
scanf("%d", &value);
insert(&head, value);
display(head);
break;
case 2:
prin ("Enter value to delete: ");
scanf("%d", &value);
delete(&head, value);
display(head);
break;
case 3:
prin ("Enter value to search: ");
scanf("%d", &value);
search(head, value);
display(head);
break;
case 4:
display(head);
break;
case 5:
prin ("Exi ng...\n");
break;
default:
prin ("Invalid choice.\n");
} while (choice != 5);
return 0;
Output :
Insert a new element
Delete an exis ng element
Search an element
Display all the elements
PROGRAM -3
Write a program to demonstrate the use of stack (implemented using linear
array) in conver ng arithme c expression from infix nota on to pos ix
nota on.
Code :
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
struct Stack {
int top;
char items[MAX];
};
void initStack(struct Stack *stack) {
stack->top = -1;
int isEmpty(struct Stack *stack) {
return stack->top == -1;
int isFull(struct Stack *stack) {
return stack->top == MAX - 1;
void push(struct Stack *stack, char value) {
if (isFull(stack)) {
prin ("Stack Overflow\n");
return;
stack->items[++stack->top] = value;
}
char pop(struct Stack *stack) {
if (isEmpty(stack)) {
prin ("Stack Underflow\n");
return '\0';
return stack->items[stack->top--];
char peek(struct Stack *stack) {
return stack->items[stack->top];
int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
if (op == '^') return 3;
return 0;
int isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
void infixToPos ix(char *infix, char *pos ix) {
struct Stack stack;
initStack(&stack);
int i, j = 0;
for (i = 0; infix[i] != '\0'; i++) {
if (isdigit(infix[i]) || isalpha(infix[i])) {
pos ix[j++] = infix[i];
} else if (infix[i] == '(') {
push(&stack, infix[i]);
} else if (infix[i] == ')') {
while (!isEmpty(&stack) && peek(&stack) != '(') {
pos ix[j++] = pop(&stack);
pop(&stack);
} else if (isOperator(infix[i])) {
while (!isEmpty(&stack) && precedence(peek(&stack)) >= precedence(infix[i])) {
pos ix[j++] = pop(&stack);
push(&stack, infix[i]);
while (!isEmpty(&stack)) {
pos ix[j++] = pop(&stack);
pos ix[j] = '\0';
int main() {
char infix[] = "a+b*(c^d-e)^(f+g*h)-i";
char pos ix[MAX];
prin ("Infix expression: %s\n", infix);
infixToPos ix(infix, pos ix);
prin ("Pos ix expression: %s\n", pos ix);
return 0;
Output :
PROGRAM -4
Program to demonstrate the use of stack (implemented using linear linked
lists) in evalua ng arithme c expression in pos ix nota on.
Code :
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
};
void initStack(struct Stack* stack) {
stack->top = NULL;
int isEmpty(struct Stack* stack) {
return stack->top == NULL;
void push(struct Stack* stack, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = stack->top;
stack->top = newNode;
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
prin ("Stack Underflow\n");
return -1;
struct Node* temp = stack->top;
int value = temp->data;
stack->top = temp->next;
free(temp);
return value;
int evaluatePos ix(char* pos ix) {
struct Stack stack;
initStack(&stack);
for (int i = 0; pos ix[i] != '\0'; i++) {
if (isdigit(pos ix[i])) {
push(&stack, pos ix[i] - '0');
} else {
int operand2 = pop(&stack);
int operand1 = pop(&stack);
switch (pos ix[i]) {
case '+': push(&stack, operand1 + operand2); break;
case '-': push(&stack, operand1 - operand2); break;
case '*': push(&stack, operand1 * operand2); break;
case '/': push(&stack, operand1 / operand2); break;
return pop(&stack);
int main() {
char pos ix[] = "53+62/*35*+";
prin ("Pos ix expression: %s\n", pos ix);
prin ("Evaluated result: %d\n", evaluatePos ix(pos ix));
return 0;
Output :
PROGRAM -5
Program to demonstra on the implementa on of various opera ons on a
linear queue represented using a linear array.
Code :
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
struct Queue {
int front;
int rear;
int arr[MAX];
};
void initQueue(struct Queue* q) {
q->front = -1;
q->rear = -1;
int isFull(struct Queue* q) {
return q->rear == MAX - 1;
int isEmpty(struct Queue* q) {
return q->front == -1;
void enqueue(struct Queue* q, int value) {
if (isFull(q)) {
prin ("Queue is full! Cannot enqueue %d\n", value);
return;
if (q->front == -1) {
q->front = 0;
}
q->arr[++q->rear] = value;
prin ("Enqueued %d\n", value);
int dequeue(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty! Cannot dequeue\n");
return -1;
int value = q->arr[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front++;
prin ("Dequeued %d\n", value);
return value;
int peek(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty! Cannot peek\n");
return -1;
return q->arr[q->front];
void display(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty! No elements to display\n");
return;
prin ("Queue elements: ");
for (int i = q->front; i <= q->rear; i++) {
prin ("%d ", q->arr[i]);
}
prin ("\n");
int main() {
struct Queue q;
initQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
display(&q);
prin ("Front element: %d\n", peek(&q));
dequeue(&q);
display(&q);
enqueue(&q, 40);
enqueue(&q, 50);
display(&q);
dequeue(&q);
dequeue(&q);
display(&q);
return 0;
Output :
PROGRAM -6
Program to demonstra on the implementa on of various opera ons on a
circular queue represented using a linear array.
Code :
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
struct Queue {
int front;
int rear;
int arr[MAX];
};
void initQueue(struct Queue* q) {
q->front = -1;
q->rear = -1;
int isFull(struct Queue* q) {
return (q->rear + 1) % MAX == q->front;
int isEmpty(struct Queue* q) {
return q->front == -1;
void enqueue(struct Queue* q, int value) {
if (isFull(q)) {
prin ("Queue is full! Cannot enqueue %d\n", value);
return;
if (q->front == -1) {
q->front = 0;
q->rear = (q->rear + 1) % MAX;
q->arr[q->rear] = value;
prin ("Enqueued %d\n", value);
}
int dequeue(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty! Cannot dequeue\n");
return -1;
int value = q->arr[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1; // Queue becomes empty
} else {
q->front = (q->front + 1) % MAX;
prin ("Dequeued %d\n", value);
return value;
int peek(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty! Cannot peek\n");
return -1;
return q->arr[q->front];
void display(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty! No elements to display\n");
return;
prin ("Queue elements: ");
int i = q->front;
while (i != q->rear) {
prin ("%d ", q->arr[i]);
i = (i + 1) % MAX;
prin ("%d\n", q->arr[q->rear]);
}
int main() {
struct Queue q;
initQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
display(&q);
prin ("Front element: %d\n", peek(&q));
dequeue(&q);
display(&q);
enqueue(&q, 40);
enqueue(&q, 50);
display(&q)
dequeue(&q);
dequeue(&q);
display(&q);
enqueue(&q, 60);
enqueue(&q, 70);
display(&q);
return 0;
Output :
PROGRAM -7
Program to demonstra on the implementa on of various opera ons on a
queue represented using a linear linked list (linked queue).
Code :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node* front;
struct Node* rear;
};
void initQueue(struct Queue* q) {
q->front = q->rear = NULL;
int isEmpty(struct Queue* q) {
return q->front == NULL;
void enqueue(struct Queue* q, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
prin ("Memory alloca on failed!\n");
return;
newNode->data = value;
newNode->next = NULL;
if (isEmpty(q)) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
prin ("Enqueued %d\n", value);
int dequeue(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty! Cannot dequeue\n");
return -1;
struct Node* temp = q->front;
int value = temp->data;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
free(temp);
prin ("Dequeued %d\n", value);
return value;
int peek(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty! Cannot peek\n");
return -1;
return q->front->data;
void display(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty! No elements to display\n");
return;
struct Node* temp = q->front;
prin ("Queue elements: ");
while (temp != NULL) {
prin ("%d ", temp->data);
temp = temp->next;
}
prin ("\n");
int main() {
struct Queue q;
initQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
display(&q);
prin ("Front element: %d\n", peek(&q));
dequeue(&q);
display(&q);
enqueue(&q, 40);
enqueue(&q, 50);
display(&q);
dequeue(&q);
dequeue(&q);
display(&q);
return 0;
Output :
PROGRAM -8
Program to illustrate the implementa on of different opera ons on a binary
search tree.
Code :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* le ;
struct Node* right;
};
// Func on to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->le = newNode->right = NULL;
return newNode;
// Func on to insert a value in the BST
struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
if (value < root->data) {
root->le = insert(root->le , value);
} else if (value > root->data) {
root->right = insert(root->right, value);
return root;
// Func on to find the minimum value node in the BST
struct Node* minValueNode(struct Node* node) {
struct Node* current = node;
while (current && current->le != NULL) {
current = current->le ;
return current;
// Func on to delete a node from the BST
struct Node* delete(struct Node* root, int value) {
if (root == NULL) {
return root;
if (value < root->data) {
root->le = delete(root->le , value);
} else if (value > root->data) {
root->right = delete(root->right, value);
} else {
if (root->le == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct Node* temp = root->le ;
free(root);
return temp;
struct Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = delete(root->right, temp->data);
return root;
// Func on to search for a value in the BST
struct Node* search(struct Node* root, int value) {
if (root == NULL || root->data == value) {
return root;
if (value < root->data) {
return search(root->le , value);
return search(root->right, value);
// Func on to perform inorder traversal of the BST
void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->le );
prin ("%d ", root->data);
inorder(root->right);
// Main func on to demonstrate the BST opera ons
int main() {
struct Node* root = NULL;
// Insert values into the BST
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
prin ("Inorder traversal: ");
inorder(root);
prin ("\n");
// Search for a value in the BST
int valueToSearch = 40;
struct Node* searchResult = search(root, valueToSearch);
if (searchResult != NULL) {
prin ("Value %d found in the BST.\n", valueToSearch);
} else {
prin ("Value %d not found in the BST.\n", valueToSearch);
// Delete a node from the BST
int valueToDelete = 20;
root = delete(root, valueToDelete);
prin ("Inorder traversal a er dele ng %d : ", valueToDelete);
inorder(root);
prin ("\n");
return 0;
Output :
PROGRAM -9
Program to illustrate the traversal of graph using breadth-first search
Code :
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 10
struct Queue {
int items[MAX_VERTICES];
int front;
int rear;
};
struct Graph {
int ver ces;
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
};
void initQueue(struct Queue* q) {
q->front = -1;
q->rear = -1;
int isEmpty(struct Queue* q) {
return q->front == -1;
void enqueue(struct Queue* q, int value) {
if (q->rear == MAX_VERTICES - 1) {
prin ("Queue is full\n");
return;
if (q->front == -1) {
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
int dequeue(struct Queue* q) {
if (isEmpty(q)) {
prin ("Queue is empty\n");
return -1;
int item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
return item;
void initGraph(struct Graph* g, int ver ces) {
g->ver ces = ver ces;
for (int i = 0; i < ver ces; i++) {
for (int j = 0; j < ver ces; j++) {
g->adjMatrix[i][j] = 0;
void addEdge(struct Graph* g, int start, int end) {
g->adjMatrix[start][end] = 1;
g->adjMatrix[end][start] = 1; // For an undirected graph
void BFS(struct Graph* g, int start) {
struct Queue q;
initQueue(&q);
int visited[MAX_VERTICES] = {0}; // Array to track visited nodes
visited[start] = 1; // Mark the start node as visited
enqueue(&q, start); // Enqueue the start node
prin ("BFS traversal star ng from node %d: ", start);
while (!isEmpty(&q)) {
int current = dequeue(&q);
prin ("%d ", current);
for (int i = 0; i < g->ver ces; i++) {
if (g->adjMatrix[current][i] == 1 && !visited[i]) {
visited[i] = 1;
enqueue(&q, i);
prin ("\n");
int main() {
struct Graph g;
initGraph(&g, 6); // Create a graph with 6 ver ces
addEdge(&g, 0, 1);
addEdge(&g, 0, 2);
addEdge(&g, 1, 3);
addEdge(&g, 1, 4);
addEdge(&g, 2, 5);
BFS(&g, 0);
return 0;
Output :
PROGRAM -10
Program to illustrate the traversal of graph using depth-first search.
Code :
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 10
// Graph structure
struct Graph {
int ver ces;
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
};
// Func on to ini alize the graph
void initGraph(struct Graph* g, int ver ces) {
g->ver ces = ver ces;
for (int i = 0; i < ver ces; i++) {
for (int j = 0; j < ver ces; j++) {
g->adjMatrix[i][j] = 0;
// Func on to add an edge to the graph
void addEdge(struct Graph* g, int start, int end) {
g->adjMatrix[start][end] = 1;
g->adjMatrix[end][start] = 1; // For an undirected graph
// DFS traversal func on using recursion
void DFS(struct Graph* g, int vertex, int visited[]) {
visited[vertex] = 1;
prin ("%d ", vertex);
for (int i = 0; i < g->ver ces; i++) {
if (g->adjMatrix[vertex][i] == 1 && !visited[i]) {
DFS(g, i, visited);
int main() {
struct Graph g;
int visited[MAX_VERTICES] = {0}; // Array to track visited nodes
initGraph(&g, 6); // Create a graph with 6 ver ces
// Add edges to the graph
addEdge(&g, 0, 1);
addEdge(&g, 0, 2);
addEdge(&g, 1, 3);
addEdge(&g, 1, 4);
addEdge(&g, 2, 5);
prin ("DFS traversal star ng from node 0: ");
DFS(&g, 0, visited);
prin ("\n");
return 0;
Output :
PROGRAM -11
Program to sort an array of integers in ascending order using bubble sort.
Code :
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
prin ("Sorted array: ");
for (int i = 0; i < n; i++) {
prin ("%d ", arr[i]);
prin ("\n");
return 0;
Output :
PROGRAM -12
Program to sort an array of integers in ascending order using selec on sort.
Code :
#include <stdio.h>
void selec onSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int minIdx = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
if (minIdx != i) {
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selec onSort(arr, n);
prin ("Sorted array: ");
for (int i = 0; i < n; i++) {
prin ("%d ", arr[i]);
prin ("\n");
return 0;
Output :
PROGRAM -13
Program to sort an array of integers in ascending order using inser on sort.
Code :
#include <stdio.h>
void inser onSort(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;
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
prin ("Before sor ng: ");
for (int i = 0; i < n; i++) prin ("%d ", arr[i]);
prin ("\n");
inser onSort(arr, n);
prin ("Sorted array: ");
for (int i = 0; i < n; i++) prin ("%d ", arr[i]);
prin ("\n");
return 0;
Output :
PROGRAM -14
Program to sort an array of integers in ascending order using merge sort.
Code :
#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int i = 0; i < n2; i++) R[i] = arr[m + 1 + i];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int n = sizeof(arr) / sizeof(arr[0]);
prin ("Before sor ng: ");
for (int i = 0; i < n; i++) prin ("%d ", arr[i]);
prin ("\n");
mergeSort(arr, 0, n - 1);
prin ("Sorted array: ");
for (int i = 0; i < n; i++) prin ("%d ", arr[i]);
prin ("\n");
return 0;
Output :
PROGRAM -15
Program to sort an array of integers in ascending order using quick sort.
Code :
#include <stdio.h>
int par on(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = par on(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
int main() {
int arr[] = {10, 80, 30, 90, 40, 50, 70};
int n = sizeof(arr) / sizeof(arr[0]);
prin ("Before sor ng: ");
for (int i = 0; i < n; i++) prin ("%d ", arr[i]);
prin ("\n");
quickSort(arr, 0, n - 1);
prin ("Sorted array: ");
for (int i = 0; i < n; i++) prin ("%d ", arr[i]);
prin ("\n");
return 0;
Output :
PROGRAM -16
Program to sort an array of integers in ascending order using heap sort.
Code :
#include <stdio.h>
void heapify(int arr[], int n, int i) {
int largest = i;
int le = 2 * i + 1;
int right = 2 * i + 2;
if (le < n && arr[le ] > arr[largest]) largest = le ;
if (right < n && arr[right] > arr[largest]) largest = right;
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
prin ("Before sor ng: ");
for (int i = 0; i < n; i++) prin ("%d ", arr[i]);
prin ("\n");
heapSort(arr, n);
prin ("Sorted array: ");
for (int i = 0; i < n; i++) prin ("%d ", arr[i]);
prin ("\n");
return 0;
Output :
PROGRAM -17
Program to demonstrate the use of linear search to search a given element in
an array.
Code :
#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) return i;
return -1;
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int key;
prin ("Enter the element you want to search: ");
scanf("%d",&key);
int result = linearSearch(arr, n, key);
if (result != -1)
prin ("Element found at index %d\n", result);
else
prin ("Element not found\n");
return 0;
Output :
PROGRAM -18
Program to demonstrate the use of binary search to search a given element in
a sorted array in ascending order.
Code :
#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) return mid;
if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
return -1;
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70};
int n = sizeof(arr) / sizeof(arr[0]);
int key;
prin ("Enter the element you want to search: ");
scanf("%d",&key);
int result = binarySearch(arr, n, key);
if (result != -1)
prin ("Element found at index %d\n", result);
else
prin ("Element not found\n");
return 0;
Output :