0% found this document useful (0 votes)
14 views

Aman

The document defines functions for implementing various sorting algorithms like quicksort, merge sort, bubble sort, etc. It takes user input for array size and elements. Then based on user's choice, it calls the respective sorting function and prints the sorted array. It also defines utility functions like swap, partition used by these sorting algorithms.

Uploaded by

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

Aman

The document defines functions for implementing various sorting algorithms like quicksort, merge sort, bubble sort, etc. It takes user input for array size and elements. Then based on user's choice, it calls the respective sorting function and prints the sorted array. It also defines utility functions like swap, partition used by these sorting algorithms.

Uploaded by

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

#include <stdio.

h>

void heapify(int arr[], int N, int i);


void printArray(int arr[], int size);
void merge(int arr[], int l, int m, int r);
void sort(int arr[], int l, int r);
void insertionSort(int arr[], int n);
void selectionSort(int arr[], int n);
void swap(int arr[], int i, int j);
int partition(int arr[], int low, int high);
void quickSort(int arr[], int low, int high);
void bubbleSort(int arr[], int n);
void printArray1(int arr[], int size);

int main()
{
printf("Enter the size of the array\n");
int N;
scanf("%d", &N);

int arr[N];
printf("Enter the elements of the array\n");
for (int i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}

for (int i = 0; i < N; i++)


{
printf("%d ", arr[i]);
}
printf("\n\n");
printf("1. Quick Sort\n");
printf("2. Merge Sort\n");
printf("3. Bubble Sort\n");
printf("4. Heap Sort\n");
printf("5. Insertion Sort\n");
printf("6. Selection Sort\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);

switch (choice)
{
case 1:
quickSort(arr, 0, N - 1);
printf("Quick Sorted array: ");
printArray(arr, N);
break;
case 2:
sort(arr, 0, N - 1);
printf("\nMerge Sorted array: ");
printArray(arr, N);
break;
case 3:
printf(" ");
bubbleSort(arr, N);
printf("Bubble Sorted array: ");
printArray(arr, N);
break;
case 4:
heapify(arr, N, 0);
printf("Heap Sorted array: ");
printArray(arr, N);
break;
case 5:
insertionSort(arr, N);
printf("Insertion Sorted array: ");
printArray(arr, N);
break;
case 6:
selectionSort(arr, N);
printf("Selection Sorted array: ");
printArray(arr, N);
break;
default:
printf("Invalid input\n");
break;
}
return 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)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, N, largest);
}
}

void printArray(int arr[], int size)


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

void merge(int arr[], int l, int m, int r)


{
int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2];

int i, j;
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

i = 0;
j = 0;
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1)


{
arr[k] = L[i];
i++;
k++;
}

while (j < n2)


{
arr[k] = R[j];
j++;
k++;
}
}

void sort(int arr[], int l, int r)


{
if (l < r)
{
int m = l + (r - l) / 2;

sort(arr, l, m);
sort(arr, m + 1, r);

merge(arr, l, m, r);
}
}
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 = j - 1;
}
arr[j + 1] = key;
printArray(arr, n);
printf("next \n");
}
}
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;
printArray(arr, n);
printf("next \n");
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
printArray(arr, j + 1);
}

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, j);
printf("next \n");
}
}
swap(arr, i + 1, high);

return (i + 1);
}

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

void bubbleSort(int arr[], int n)


{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{

for (j = 0; j < n - i - 1; j++)


{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
printArray(arr, n);
printf("next \n");
}
}
}

void printArray1(int arr[], int size)


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

You might also like