Implementation of Different Sorting Algorithm Like Insertion, Quick, Heap, Bubble
Implementation of Different Sorting Algorithm Like Insertion, Quick, Heap, Bubble
h>
// Function prototypes
void printArray(int arr[], int size);
void swap(int *a, int *b);
// Sorting algorithms
void insertionSort(int arr[], int n);
void quickSort(int arr[], int low, int high);
void heapSort(int arr[], int n);
void bubbleSort(int arr[], int n);
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
// Insertion Sort
insertionSort(arr, n);
printf("\nSorted array using Insertion Sort: ");
printArray(arr, n);
// Quick Sort
quickSort(arr2, 0, n - 1);
printf("\nSorted array using Quick Sort: ");
printArray(arr2, n);
// Heap Sort
heapSort(arr3, n);
printf("\nSorted array using Heap Sort: ");
printArray(arr3, n);
// Bubble Sort
bubbleSort(arr4, n);
printf("\nSorted array using Bubble Sort: ");
printArray(arr4, n);
return 0;
}
// Insertion Sort
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
// Quick Sort
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
// Heap Sort
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
// 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])
swap(&arr[j], &arr[j + 1]);
}