Bubble Sort
#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;
}
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
bubbleSort(arr, n);
printf("Bubble Sorted array:\n");
printArray(arr, n);
return 0;
}
Output:
Original array: 64 34 25 12 22 11 90
Bubble Sorted array: 11 12 22 25 34 64 90
Insertion Sort
#include <stdio.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 printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
insertionSort(arr, n);
printf("Insertion Sorted array:\n");
printArray(arr, n);
return 0;
}
Output:
Original array: 64 34 25 12 22 11 90
Insertion Sorted array: 11 12 22 25 34 64 90
Selection Sort
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
selectionSort(arr, n);
printf("Selection Sorted array:\n");
printArray(arr, n);
return 0;
}
Output:
Original array: 64 34 25 12 22 11 90
Selection Sorted array: 11 12 22 25 34 64 90
Shell Sort
#include <stdio.h>
void shellSort(int arr[], int n) {
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
shellSort(arr, n);
printf("Shell Sorted array:\n");
printArray(arr, n);
return 0;
}
Output:
Original array: 64 34 25 12 22 11 90
Shell Sorted array: 11 12 22 25 34 64 90
Radix Sort
#include <stdio.h>
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
void countingSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
void radixSort(int arr[], int n) {
int max = getMax(arr, n);
for (int exp = 1; max / exp > 0; exp *= 10)
countingSort(arr, n, exp);
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
radixSort(arr, n);
printf("Radix Sorted array:\n");
printArray(arr, n);
return 0;
}
Output:
Original array: 64 34 25 12 22 11 90
Radix Sorted array: 11 12 22 25 34 64 90
Quick Sort
#include <stdio.h>
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; 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;
int pi = i + 1;
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
quickSort(arr, 0, n - 1);
printf("Quick Sorted array:\n");
printArray(arr, n);
return 0;
}
Output:
Original array:
64 34 25 12 22 11 90
Quick Sorted array:
11 12 22 25 34 64 90
Linear Search
#include <stdio.h>
int linearSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 30;
int result = linearSearch(arr, n, x);
if (result == -1)
printf("Element not found.\n");
else
printf("Element found at index %d.\n", result);
return 0;
}
Output:
Element found at index 2.
Binary Search
#include <stdio.h>
int binarySearch(int arr[], int low, int high, int x) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 40;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1)
printf("Element not found.\n");
else
printf("Element found at index %d.\n", result);
return 0;
}
Output:
Element found at index 3.
Array Implementation of Stack
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
// Stack structure
struct Stack {
int arr[MAX];
int top;
};
// Function to initialize the stack
void initStack(struct Stack *stack) {
stack->top = -1;
}
// Check if the stack is full
int isFull(struct Stack *stack) {
return stack->top == MAX - 1;
}
// Check if the stack is empty
int isEmpty(struct Stack *stack) {
return stack->top == -1;
}
// Push operation
void push(struct Stack *stack, int value) {
if (isFull(stack)) {
printf("Stack Overflow\n");
} else {
stack->arr[++(stack->top)] = value;
}
}
// Pop operation
int pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
} else {
return stack->arr[(stack->top)--];
}
}
// Peek operation
int peek(struct Stack *stack) {
if (isEmpty(stack)) {
return -1;
} else {
return stack->arr[stack->top];
}
}
int main() {
struct Stack stack;
initStack(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("Top of Stack: %d\n", peek(&stack));
printf("Popped: %d\n", pop(&stack));
printf("Popped: %d\n", pop(&stack));
printf("Top of Stack: %d\n", peek(&stack));
return 0;
}
Output:
Top of Stack: 30
Popped: 30
Popped: 20
Top of Stack: 10
Linked List Implementation of Stack
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Stack structure
struct Stack {
struct Node* top;
};
// Initialize stack
void initStack(struct Stack* stack) {
stack->top = NULL;
}
// Check if stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == NULL;
}
// Push operation
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;
}
// Pop operation
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
} else {
struct Node* temp = stack->top;
int value = temp->data;
stack->top = stack->top->next;
free(temp);
return value;
}
}
// Peek operation
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
return -1;
} else {
return stack->top->data;
}
}
int main() {
struct Stack stack;
initStack(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("Top of Stack: %d\n", peek(&stack));
printf("Popped: %d\n", pop(&stack));
printf("Popped: %d\n", pop(&stack));
printf("Top of Stack: %d\n", peek(&stack));
return 0;
}
Output:
Top of Stack: 30
Popped: 30
Popped: 20
Top of Stack: 10
Evaluation of Postfix Expression
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Stack structure
struct Stack {
int* arr;
int top;
int size;
};
// Initialize the stack
void initStack(struct Stack* stack, int size) {
stack->arr = (int*)malloc(size * sizeof(int));
stack->top = -1;
stack->size = size;
}
// Push operation
void push(struct Stack* stack, int value) {
stack->arr[++stack->top] = value;
}
// Pop operation
int pop(struct Stack* stack) {
return stack->arr[stack->top--];
}
// Function to evaluate postfix expression
int evaluatePostfix(char* exp) {
struct Stack stack;
initStack(&stack, 100);
for (int i = 0; exp[i] != '\0'; i++) {
if (isdigit(exp[i])) {
push(&stack, exp[i] - '0');
} else {
int operand2 = pop(&stack);
int operand1 = pop(&stack);
switch (exp[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 exp[] = "23*54*+9-";
printf("Result of Postfix Evaluation: %d\n",
evaluatePostfix(exp));
return 0;
}
Output:
Result of Postfix Evaluation: 17
Array Implementation of Circular Queue
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
// Queue structure
struct Queue {
int arr[MAX];
int front;
int rear;
};
// Initialize the queue
void initQueue(struct Queue* queue) {
queue->front = queue->rear = -1;
}
// Check if the queue is full
int isFull(struct Queue* queue) {
return (queue->rear + 1) % MAX == queue->front;
}
// Check if the queue is empty
int isEmpty(struct Queue* queue) {
return queue->front == -1;
}
// Enqueue operation
void enqueue(struct Queue* queue, int value) {
if (isFull(queue)) {
printf("Queue Overflow\n");
return;
}
if (isEmpty(queue)) {
queue->front = queue->rear = 0;
} else {
queue->rear = (queue->rear + 1) % MAX;
}
queue->arr[queue->rear] = value;
}
// Dequeue operation
int dequeue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue Underflow\n");
return -1;
}
int value = queue->arr[queue->front];
if (queue->front == queue->rear) {
queue->front = queue->rear = -1;
} else {
queue->front = (queue->front + 1) % MAX;
}
return value;
}
// Peek operation
int peek(struct Queue* queue) {
if (isEmpty(queue)) {
return -1;
}
return queue->arr[queue->front];
}
int main() {
struct Queue queue;
initQueue(&queue);
enqueue(&queue, 10);
enqueue(&queue, 20);
enqueue(&queue, 30);
printf("Dequeued: %d\n", dequeue(&queue));
printf("Front of Queue: %d\n", peek(&queue));
enqueue(&queue, 40);
enqueue(&queue, 50);
printf("Dequeued: %d\n", dequeue(&queue));
return 0;
}
Output:
Dequeued: 10
Front of Queue: 20
Dequeued: 20
Linked List Implementation of Priority Queue
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
int priority;
struct Node* next;
};
// Priority Queue structure
struct PriorityQueue {
struct Node* front;
};
// Initialize the priority queue
void initQueue(struct PriorityQueue* pq) {
pq->front = NULL;
}
// Check if the queue is empty
int isEmpty(struct PriorityQueue* pq) {
return pq->front == NULL;
}
// Enqueue operation with priority
void enqueue(struct PriorityQueue* pq, int value, int priority) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->priority = priority;
newNode->next = NULL;
if (pq->front == NULL || pq->front->priority > priority) {
newNode->next = pq->front;
pq->front = newNode;
} else {
struct Node* temp = pq->front;
while (temp->next != NULL && temp->next->priority <=
priority) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
// Dequeue operation
int dequeue(struct PriorityQueue* pq) {
if (isEmpty(pq)) {
printf("Queue Underflow\n");
return -1;
}
struct Node* temp = pq->front;
int value = temp->data;
pq->front = pq->front->next;
free(temp);
return value;
}
// Peek operation
int peek(struct PriorityQueue* pq) {
if (isEmpty(pq)) {
return -1;
}
return pq->front->data;
}
int main() {
struct PriorityQueue pq;
initQueue(&pq);
enqueue(&pq, 10, 2);
enqueue(&pq, 20, 1);
enqueue(&pq, 30, 3);
printf("Dequeued: %d\n", dequeue(&pq));
printf("Front of Queue: %d\n", peek(&pq));
return 0;
}
Output:
Dequeued: 20
Front of Queue: 10
Double-Ended Queue (Deque)
#include <stdio.h>
#include <stdlib.h>
struct Deque {
int* arr;
int front;
int rear;
int size;
int capacity;
};
void initDeque(struct Deque* deque, int capacity) {
deque->capacity = capacity;
deque->arr = (int*)malloc(capacity * sizeof(int));
deque->front = -1;
deque->rear = -1;
deque->size = 0;
}
int isFull(struct Deque* deque) {
return deque->size == deque->capacity;
}
int isEmpty(struct Deque* deque) {
return deque->size == 0;
}
void enqueueFront(struct Deque* deque, int value) {
if (isFull(deque)) {
printf("Deque Overflow\n");
return;
}
if (deque->front == -1) {
deque->front = deque->rear = 0;
} else {
deque->front = (deque->front - 1 + deque->capacity) % deque-
>capacity;
}
deque->arr[deque->front] = value;
deque->size++;
}
void enqueueRear(struct Deque* deque, int value) {
if (isFull(deque)) {
printf("Deque Overflow\n");
return;
}
if (deque->rear == -1) {
deque->front = deque->rear = 0;
} else {
deque->rear = (deque->rear + 1) % deque->capacity;
}
deque->arr[deque->rear] = value;
deque->size++;
}
int dequeueFront(struct Deque* deque) {
if (isEmpty(deque)) {
printf("Deque Underflow\n");
return -1;
}
int value = deque->arr[deque->front];
if (deque->front == deque->rear) {
deque->front = deque->rear = -1;
} else {
deque->front = (deque->front + 1) % deque->capacity;
}
deque->size--;
return value;
}
int dequeueRear(struct Deque* deque) {
if (isEmpty(deque)) {
printf("Deque Underflow\n");
return -1;
}
int value = deque->arr[deque->rear];
if (deque->front == deque->rear) {
deque->front = deque->rear = -1;
} else {
deque->rear = (deque->rear - 1 + deque->capacity) % deque-
>capacity;
}
deque->size--;
return value;
}
int main() {
struct Deque deque;
initDeque(&deque, 5);
enqueueRear(&deque, 10);
enqueueRear(&deque, 20);
enqueueFront(&deque, 30);
enqueueRear(&deque, 40);
printf("Dequeued from front: %d\n", dequeueFront(&deque));
printf("Dequeued from rear: %d\n", dequeueRear(&deque));
return 0;
}
Output:
Dequeued from front: 30
Dequeued from rear: 40
Singly Linked List (SLL)
#include <stdio.h>
#include <stdlib.h>
// Node structure for Singly Linked List
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Insert at the end
void insert(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// Display the list
void display(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
return;
}
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Delete a node
void delete(struct Node** head, int key) {
struct Node* temp = *head;
struct Node* prev = NULL;
if (temp != NULL && temp->data == key) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
// Search for a node
int search(struct Node* head, int key) {
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == key)
return 1;
temp = temp->next;
}
return 0;
}
// Count the number of nodes
int count(struct Node* head) {
int count = 0;
struct Node* temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
// Reverse the list
void reverse(struct Node** head) {
struct Node* prev = NULL;
struct Node* current = *head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
int main() {
struct Node* head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
printf("Original List: ");
display(head);
delete(&head, 20);
printf("After Deletion (20): ");
display(head);
printf("Search for 30: %s\n", search(head, 30) ? "Found" : "Not
Found");
printf("Node Count: %d\n", count(head));
reverse(&head);
printf("Reversed List: ");
display(head);
return 0;
}
Output:
Original List: 10 -> 20 -> 30 -> NULL
After Deletion (20): 10 -> 30 -> NULL
Search for 30: Found
Node Count: 2
Reversed List: 30 -> 10 -> NULL
Circular Linked List (CLL)
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = newNode; // Points to itself
return newNode;
}
void insert(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}
}
void display(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(back to head)\n");
}
void delete(struct Node** head, int key) {
if (*head == NULL) return;
struct Node *temp = *head, *prev = NULL;
if (temp->data == key) {
if (temp->next == *head) {
free(temp);
*head = NULL;
} else {
while (temp->next != *head) {
temp = temp->next;
}
*head = (*head)->next;
free(temp);
}
return;
}
do {
prev = temp;
temp = temp->next;
if (temp->data == key) {
prev->next = temp->next;
free(temp);
return;
}
} while (temp != *head);
}
int count(struct Node* head) {
if (head == NULL) return 0;
int count = 1;
struct Node* temp = head->next;
while (temp != head) {
count++;
temp = temp->next;
}
return count;
}
int main() {
struct Node* head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
printf("Circular List: ");
display(head);
delete(&head, 20);
printf("After Deletion (20): ");
display(head);
printf("Node Count: %d\n", count(head));
return 0;
}
Output:
Circular List: 10 -> 20 -> 30 -> (back to head)
After Deletion (20): 10 -> 30 -> (back to head)
Node Count: 2