#include <stdio.
h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int lb, int ub) {
// Initialize pivot to be the first element
int p = arr[lb];
int i = lb;
int j = ub;
while (i < j) {
// Find the first element greater than the pivot
while (p>=arr[i]) {
i++;
}
// Find the first element smaller than the pivot
while (p<arr[j]) {
j--;
}
if (i < j) {
swap(&arr[i], &arr[j]);
}
}
swap(&arr[lb], &arr[j]);
return j;
}
void quickSort(int arr[], int lb, int ub) {
if (lb < ub) {
int pi = partition(arr, lb, ub);
// Recursively call quickSort() for left and right half based on Partition Index
quickSort(arr, lb, pi - 1);
quickSort(arr, pi + 1, ub);
}
}
int main() {
int arr[] = { 4, 6, 5, 3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("sorted array is:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}