#include <stdio.h>
#include <string.h>
// Comparison based sort: Bubble Sort
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;
}
}
}
}
// Non comparison based sort: Counting Sort (assumes max value known)
void countingSort(int arr[], int n, int max_val) {
int count[max_val + 1];
memset(count, 0, sizeof(count));
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
int index = 0;
for (int i = 0; i <= max_val; i++) {
while (count[i] > 0) {
arr[index++] = i;
count[i]--;
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr1[] = {4, 2, 7, 1, 3};
int arr2[] = {4, 2, 7, 1, 3};
int n = 5;
int max_val = 7;
bubbleSort(arr1, n);
printf("Bubble Sort: ");
printArray(arr1, n);
countingSort(arr2, n, max_val);
printf("Counting Sort: ");
printArray(arr2, n);
return 0;
}if (arr[j] > arr[j + 1]) {
compare adjacent elements to decide swap
count occurrences of each element
while (count[i] > 0) { arr[index++] = i; count[i]--; }
place elements back in sorted order using counts
Bubble Sort: 1 2 3 4 7
Counting Sort: 1 2 3 4 7