0% found this document useful (0 votes)
22 views3 pages

Implementation of Different Sorting Algorithm Like Insertion, Quick, Heap, Bubble

Uploaded by

Lakshya Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Implementation of Different Sorting Algorithm Like Insertion, Quick, Heap, Bubble

Uploaded by

Lakshya Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

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]);

printf("Original array: ");


printArray(arr, n);

// Insertion Sort
insertionSort(arr, n);
printf("\nSorted array using Insertion Sort: ");
printArray(arr, n);

// Reset array for other sorting algorithms


int arr2[] = {12, 11, 13, 5, 6, 7};

// Quick Sort
quickSort(arr2, 0, n - 1);
printf("\nSorted array using Quick Sort: ");
printArray(arr2, n);

// Reset array for other sorting algorithms


int arr3[] = {12, 11, 13, 5, 6, 7};

// Heap Sort
heapSort(arr3, n);
printf("\nSorted array using Heap Sort: ");
printArray(arr3, n);

// Reset array for other sorting algorithms


int arr4[] = {12, 11, 13, 5, 6, 7};

// Bubble Sort
bubbleSort(arr4, n);
printf("\nSorted array using Bubble Sort: ");
printArray(arr4, n);

return 0;
}

// Function to print an array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Function to swap two elements


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Insertion Sort
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// Quick Sort
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {


if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

// Heap Sort
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--) {


swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}

void heapify(int arr[], int n, int i) {


int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;

if (l < n && arr[l] > arr[largest])


largest = l;

if (r < n && arr[r] > arr[largest])


largest = r;

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]);
}

You might also like