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

Sorting Programs With Outputs

Sorting Programs With Outputs

Uploaded by

nandini.k
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)
4 views6 pages

Sorting Programs With Outputs

Sorting Programs With Outputs

Uploaded by

nandini.k
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

SORTING TECHNIQUE PROGRAMS WITH OUTPUT

1. QUICK SORT

Program:

#include

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

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

int pivot = arr[high];

int i = (low - 1);

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

if (arr[j] < pivot) {

i++;

swap(&arr;[i], &arr;[j]);

swap(&arr;[i + 1], &arr;[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);
}

int main() {

int arr[] = {10, 7, 8, 9, 1, 5};

int n = 6;

quickSort(arr, 0, n - 1);

printf("Sorted array: ");

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

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

return 0;

Output:

Sorted array: 1 5 7 8 9 10

2. MERGE SORT

Program:

#include

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

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

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

L[i] = arr[l + i];

for (int j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

int i = 0, j = 0, k = l;

while (i < n1 && j < n2) {


if (L[i] <= R[j])

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

else

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

while (i < n1)

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

while (j < n2)

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

void mergeSort(int arr[], int l, int r) {

if (l < r) {

int m = (l + r) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

int main() {

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

int n = 6;

mergeSort(arr, 0, n - 1);

printf("Sorted array: ");

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

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

}
Output:

Sorted array: 5 6 7 11 12 13

3. HEAP SORT

Program:

#include

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[] = {4, 10, 3, 5, 1};

int n = 5;

heapSort(arr, n);

printf("Sorted array: ");

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

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

Output:

Sorted array: 1 3 4 5 10

4. SHELL SORT

Program:

#include

void shellSort(int arr[], int n) {

for (int gap = n / 2; gap > 0; gap /= 2) {

for (int i = gap; i < n; i++) {

int temp = arr[i];

int j;

for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)

arr[j] = arr[j - gap];

arr[j] = temp;

}
}

int main() {

int arr[] = {12, 34, 54, 2, 3};

int n = 5;

shellSort(arr, n);

printf("Sorted array: ");

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

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

Output:

Sorted array: 2 3 12 34 54

You might also like