Practical 1
Question: Write a program to implement “Bubble Sort” Algorithm in C
programming Language?
#include <stdio.h>
int array[10];
void input_array(int[], int);
void print_array(int[], int);
void bubble_sort(int[], int);
void main()
{
input_array(array, 5);
bubble_sort(array, 5);
print_array(array, 5);
}
void input_array(int A[], int N)
{
int i;
printf("Enter the values for sorting :-\n");
for (i = 1; i <= N; i++)
{
printf("Value %d :", i);
scanf("%d", &A[i]);
}
}
void print_array(int A[], int N)
{
int i;
printf("\nSorted array is : {");
for (i = 1; i <= N; i++)
{
printf("%d, ", A[i]);
}
printf("}");
}
void bubble_sort(int A[], int N)
{
int temp, i, j;
for (i = 1; i <= N - 1; i++)
{
for (j = 1; j <= N - i; j++)
{
if (A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
Output:
Practical 2
Question: Write a program to implement “Insertion Sort” Algorithm in C
programming Language?
#include <stdio.h>
int array[10];
void input_array(int[], int);
void print_array(int[], int);
void insertion_sort(int[], int);
void main()
{
input_array(array, 5);
insertion_sort(array, 5);
print_array(array, 5);
}
void input_array(int A[], int N)
{
int i;
printf("\nSorted array is : {");
for (i = 1; i <= N; i++)
{
if (i != N)
printf("%d, ", A[i]);
else
printf("%d}", A[i]);
}
}
void print_array(int A[], int N)
{
int i;
printf("\nSorted array is : {");
for (i = 1; i <= N; i++)
{
printf("%d, ", A[i]);
}
printf("}");
}
void insertion_sort(int A[], int N)
{
int key, i, j;
for (j = 1; j <= N; j++)
{
key = A[j];
i = j - 1;
while (i > 0 && A[i] > key)
{
A[i + 1] = A[i];
i--;
}
A[i + 1] = key;
}
}
Output:
Practical 3
Question: Write a program to implement “Selection Sort” Algorithm in C
programming Language?
#include <stdio.h>
int array[10];
void input_array(int[], int);
void print_array(int[], int);
void selection_sort(int[], int);
int minimum(int[], int, int);
void main()
{
input_array(array, 5);
selection_sort(array, 5);
print_array(array, 5);
}
void input_array(int A[], int N)
{
int i;
printf("Enter the values for sorting :-\n");
for (i = 1; i <= N; i++)
{
printf("Value %d :", i);
scanf("%d", &A[i]);
}
}
void print_array(int A[], int N)
{
int i;
printf("\nSorted array is : {");
for (i = 1; i <= N; i++)
{
printf("%d, ", A[i]);
}
printf("}");
}
void selection_sort(int A[], int N)
{
int k;
int loc;
int temp;
for (k = 1; k <= N - 1; k++)
{
loc = minimum(A, k, N);
temp = A[k];
A[k] = A[loc];
A[loc] = temp;
}
}
int minimum(int A[], int K, int N)
{
int j;
int min = A[K];
int loc = K;
for (j = K + 1; j <= N; j++)
{
if (min > A[j])
{
min = A[j];
loc = j;
}
}
return loc;
}
Output:
Practical 4
Question: Write a program to implement “Merge Sort” Algorithm in C
programming Language?
#include <stdio.h>
#include <limits.h>
int array[10];
void input_array(int[], int);
void print_array(int[], int);
void merge_sort(int[], int, int);
void merge(int[], int, int, int);
void main()
{
input_array(array, 5);
merge_sort(array, 1, 5);
print_array(array, 5);
}
void input_array(int A[], int N)
{
int i;
printf("Enter the values for sorting :-\n");
for (i = 1; i <= N; i++)
{
printf("Value %d :", i);
scanf("%d", &A[i]);
}
}
void print_array(int A[], int N)
{
int i;
printf("\nSorted array is : {");
for (i = 1; i <= N; i++)
{
printf("%d, ", A[i]);
}
printf("}");
}
void merge_sort(int A[], int P, int R)
{
int Q;
if (P < R)
{
Q = (P + R) / 2;
merge_sort(A, P, Q);
merge_sort(A, Q + 1, R);
print_array(array, 5);
merge(A, P, Q, R);
}
}
void merge(int A[], int P, int Q, int R)
{
int n1, n2, i, j, k;
n1 = Q - P + 1;
n2 = R - Q;
int LEFT[n1 + 1], RIGHT[n2 + 1];
for (i = 1; i <= n1; i++)
LEFT[i] = A[P + i - 1];
for (j = 1; j <= n1; j++)
RIGHT[j] = A[Q + j];
LEFT[n1 + 1] = INT_MAX;
RIGHT[n2 + 1] = INT_MAX;
i = 1, j = 1;
for (k = P; k <= R; k++)
{
if (LEFT[i] <= RIGHT[j])
{
A[k] = LEFT[i];
i++;
}
else
{
A[k] = RIGHT[j];
j++;
}
}
}
Output:
Practical 5
Question: Write a program to implement “Quick Sort” Algorithm in C
programming Language?
#include <stdio.h>
#include <limits.h>
int array[10];
void input_array(int[], int);
void print_array(int[], int);
void quick_sort(int[], int, int);
int partition(int[], int, int);
void main()
{
input_array(array, 5);
quick_sort(array, 1, 5);
print_array(array, 5);
}
void input_array(int A[], int N)
{
int i;
printf("Enter the values for sorting :-\n");
for (i = 1; i <= N; i++)
{
printf("Value %d :", i);
scanf("%d", &A[i]);
}
}
void print_array(int A[], int N)
{
int i;
printf("\nSorted array is : {");
for (i = 1; i <= N; i++)
{
if (i != N)
printf("%d, ", A[i]);
else
printf("%d}", A[i]);
}
}
void quick_sort(int A[], int P, int R)
{
int Q;
if (P < R)
{
Q = partition(A, P, R);
quick_sort(A, P, Q - 1);
quick_sort(A, Q + 1, R);
}
}
int partition(int A[], int P, int R)
{
int x, i, j, temp;
x = A[ R ];
i = P - 1;
for (j = P; j <= R - 1; j++)
{
if (A[ j ] <= x)
{
i++;
temp = A[ i ];
A[ i ] = A[ j ];
A[ j ] = temp;
}
}
temp = A[ i + 1 ];
A[ i + 1 ] = A[ R ];
A[ R ] = temp;
return ( i + 1 );
}
Output:
Practical 6
Question: Write a program to implement “Stack” using array in C
programming Language?
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct Stack {
int array[MAX_SIZE];
int top;
};
void initialize(struct Stack *stack) {
stack->top = -1;
}
int isEmpty(struct Stack *stack) {
return (stack->top == -1);
}
int isFull(struct Stack *stack) {
return (stack->top == MAX_SIZE - 1);
}
void push(struct Stack *stack, int value) {
if (isFull(stack)) {
printf("Stack overflow! Cannot push element.\n");
return;
}
stack->array[++stack->top] = value;
printf("%d pushed to stack\n", value);
}
int pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack underflow! Cannot pop element.\n");
return -1;
}
return stack->array[stack->top--];
}
int peek(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty! Cannot peek.\n");
return -1;
}
return stack->array[stack->top];
}
int main() {
struct Stack myStack;
initialize(&myStack);
push(&myStack, 10);
push(&myStack, 20);
push(&myStack, 30);
printf("Top element: %d\n", peek(&myStack));
printf("%d popped from stack\n", pop(&myStack));
printf("%d popped from stack\n", pop(&myStack));
printf("%d popped from stack\n", pop(&myStack));
printf("Is the stack empty? %s\n", isEmpty(&myStack) ? "Yes" :
"No");
return 0;
}
Output:
Practical 7
Question: Write a program to implement “Stack” using linked list in C
programming Language?
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
};
void initialize(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));
if (newNode == NULL) {
printf("Memory allocation failed! Cannot push element.\n");
return;
}
newNode->data = value;
newNode->next = stack->top;
stack->top = newNode;
printf("%d pushed to stack\n", value);
}
int pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack underflow! Cannot pop element.\n");
return -1;
}
struct Node* poppedNode = stack->top;
int poppedValue = poppedNode->data;
stack->top = poppedNode->next;
free(poppedNode);
return poppedValue;
}
int peek(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty! Cannot peek.\n");
return -1;
}
return stack->top->data;
}
int main() {
struct Stack myStack;
initialize(&myStack);
push(&myStack, 15);
push(&myStack, 25);
push(&myStack, 35);
printf("Top element: %d\n", peek(&myStack));
printf("%d popped from stack\n", pop(&myStack));
printf("%d popped from stack\n", pop(&myStack));
printf("%d popped from stack\n", pop(&myStack));
printf("Is the stack empty? %s\n", isEmpty(&myStack) ? "Yes" :
"No");
return 0;
}
Output:
Practical 8
Question: Write a program to implement “Queue” using array in C
programming Language?
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct Queue {
int array[MAX_SIZE];
int front, rear;
};
void initialize(struct Queue *queue) {
queue->front = -1;
queue->rear = -1;
}
int isEmpty(struct Queue *queue) {
return (queue->front == -1 && queue->rear == -1);
}
int isFull(struct Queue *queue) {
return ((queue->rear + 1) % MAX_SIZE == queue->front);
}
void enqueue(struct Queue *queue, int value) {
if (isFull(queue)) {
printf("Queue overflow! Cannot enqueue element.\n");
return;
}
if (isEmpty(queue)) {
queue->front = 0; // If the queue is empty, set front to 0
}
queue->rear = (queue->rear + 1) % MAX_SIZE;
queue->array[queue->rear] = value;
printf("%d enqueued to queue\n", value);
}
int dequeue(struct Queue *queue) {
int removedValue;
if (isEmpty(queue)) {
printf("Queue underflow! Cannot dequeue element.\n");
return -1;
}
removedValue = queue->array[queue->front];
if (queue->front == queue->rear) {
queue->front = -1;
queue->rear = -1;
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
}
return removedValue;
}
int front(struct Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty! Cannot get front element.\n");
return -1;
}
return queue->array[queue->front];
}
int main() {
struct Queue myQueue;
initialize(&myQueue);
enqueue(&myQueue, 10);
enqueue(&myQueue, 20);
enqueue(&myQueue, 30);
printf("Front element: %d\n", front(&myQueue));
printf("%d dequeued from queue\n", dequeue(&myQueue));
printf("%d dequeued from queue\n", dequeue(&myQueue));
printf("%d dequeued from queue\n", dequeue(&myQueue));
printf("Is the queue empty? %s\n", isEmpty(&myQueue) ? "Yes" :
"No");
return 0;
}
Output:
Practical 9
Question: Write a program to implement “Queue” using linked list in C
programming Language?
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node* front;
struct Node* rear;
};
void initialize(struct Queue* queue) {
queue->front = NULL;
queue->rear = NULL;
}
int isEmpty(struct Queue* queue) {
return (queue->front == NULL && queue->rear == NULL);
}
void enqueue(struct Queue* queue, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed! Cannot enqueue element.\n");
return;
}
newNode->data = value;
newNode->next = NULL;
if (isEmpty(queue)) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
printf("%d enqueued to queue\n", value);
}
int dequeue(struct Queue* queue) {
int removedValue;
if (isEmpty(queue)) {
printf("Queue underflow! Cannot dequeue element.\n");
return -1;
}
struct Node* removedNode = queue->front;
removedValue = removedNode->data;
if (queue->front == queue->rear) {
queue->front = NULL;
queue->rear = NULL;
} else {
queue->front = removedNode->next;
}
free(removedNode);
return removedValue;
}
int front(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty! Cannot get front element.\n");
return -1;
}
return queue->front->data;
}
int main() {
struct Queue myQueue;
initialize(&myQueue);
enqueue(&myQueue, 15);
enqueue(&myQueue, 25);
enqueue(&myQueue, 35);
printf("Front element: %d\n", front(&myQueue));
printf("%d dequeued from queue\n", dequeue(&myQueue));
printf("%d dequeued from queue\n", dequeue(&myQueue));
printf("%d dequeued from queue\n", dequeue(&myQueue));
printf("Is the queue empty? %s\n", isEmpty(&myQueue) ? "Yes" :
"No");
return 0;
}
Output:
Practical 10
Question: Write a program to implement “Binary Tree” in C programming
Language?
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
struct TreeNode* createNode(int data) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct
TreeNode));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct TreeNode* insert(struct TreeNode* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
void inorderTraversal(struct TreeNode* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
void freeTree(struct TreeNode* root) {
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
}
}
int main() {
struct TreeNode* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
freeTree(root);
return 0;
}
Output: