0% found this document useful (0 votes)
2 views6 pages

Merge, Heap, Quick Sorts

The document contains C programs implementing three sorting algorithms: Merge Sort, Heap Sort, and Quick Sort. Each algorithm includes a main function that demonstrates sorting an array of integers, initially set to {12, 11, 13, 5, 6, 7}, and outputs the original and sorted arrays. All sorting methods successfully sort the array to {5, 6, 7, 11, 12, 13}.

Uploaded by

mdshakeer811
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)
2 views6 pages

Merge, Heap, Quick Sorts

The document contains C programs implementing three sorting algorithms: Merge Sort, Heap Sort, and Quick Sort. Each algorithm includes a main function that demonstrates sorting an array of integers, initially set to {12, 11, 13, 5, 6, 7}, and outputs the original and sorted arrays. All sorting methods successfully sort the array to {5, 6, 7, 11, 12, 13}.

Uploaded by

mdshakeer811
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

C PROGRAM TO PERFORM SORTING METHODS:

---MERGE SORT:---
#include <stdio.h>

#include <stdlib.h>
void merge(int arr[], int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;

int L[n1], R[n2];


for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

i = 0;
j = 0;
k = left;
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 mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}}
int main() {

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


int arrSize = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < arrSize; i++) {
printf("%d ", arr[i]);

}
mergeSort(arr, 0, arrSize - 1);

printf("\nSorted array: ");


for (int i = 0; i < arrSize; i++) {

printf("%d ", arr[i]);


}
return 0;
}
OUTPUT:

Original array: 12 11 13 5 6 7

Sorted array: 5 6 7 11 12 13
---HEAP SORT:---
#include <stdio.h>
#include <stdlib.h>

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


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])

largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
int temp = arr[i];

arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}

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--) {
int temp = arr[0];

arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arrSize = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < arrSize; i++) {

printf("%d ", arr[i]);


}
heapSort(arr, arrSize);
printf("\nSorted array: ");
for (int i = 0; i < arrSize; i++) {

printf("%d ", arr[i]);


}
return 0;
}
OUTPUT:

Original array: 12 11 13 5 6 7
Sorted array: 5 6 7 11 12 13
---QUICK SORT:---
#include <stdio.h>
#include <stdlib.h>

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++;
int temp = arr[i];
arr[i] = arr[j];

arr[j] = temp;
}}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;

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);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arrSize = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


for (int i = 0; i < arrSize; i++) {
printf("%d ", arr[i]);
}
quickSort(arr, 0, arrSize - 1);

printf("\nSorted array: ");


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

}
OUTPUT:
Original array: 12 11 13 5 6 7
Sorted array: 5 6 7 11 12 13

You might also like