SOURCE CODE:
#include <stdio.h>
// Function to swap two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
// Partition function to select pivot and arrange elements
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Taking the last element as the pivot
int i = low - 1; // Index for the smaller element
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]); // Swap elements
swap(&arr[i + 1], &arr[high]); // Place pivot in correct position
return i + 1;
// Quick Sort function
void quickSort(int arr[], int low, int high) {
if (low < high) {
// Partition the array and get the pivot index
int pi = partition(arr, low, high);
// Recursively sort the two sub-arrays
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: \n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("Original array: \n");
printArray(arr, n);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
OUTPUT :