0% found this document useful (0 votes)
4 views1 page

Quick

The document contains a C program that implements the QuickSort algorithm to sort an array of integers. It includes functions for swapping elements, partitioning the array, and printing the array before and after sorting. The main function demonstrates the sorting of a predefined array of integers.
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)
4 views1 page

Quick

The document contains a C program that implements the QuickSort algorithm to sort an array of integers. It includes functions for swapping elements, partitioning the array, and printing the array before and after sorting. The main function demonstrates the sorting of a predefined array of integers.
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/ 1

#include <stdio.

h>

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


int t = *a;
*a = *b;
*b = t;
}

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


int pivot = array[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
}

void quickSort(int array[], int low, int high) {


if (low < high) {
int pi = partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}

void printArray(int array[], int size) {


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

int main() {
int data[] = {8, 7, 2, 1, 0, 9, 6};
int n = sizeof(data) / sizeof(data[0]);
printf("Unsorted Array\n");
printArray(data, n);
quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n");
printArray(data, n);
}

You might also like