Quick sort


//
// https://www.programiz.com/c-programming/online-compiler/
#include <stdio.h>

void quick_sort(int b[], int low, int high);
int partition(int b[], int low, int high);

int main() {

    int a[]={89, 8, 91, 99, 82, 22, 55, 31, 93, 23, 30, 84, 41, 7, 57, 33, 47, 19, 85, 77, 76, 53, 61, 42, 36};  
    int len=(sizeof(a) / sizeof(int));

    printf("Original + sorted array:\n");
    for (int i = 0; i < len; i++) {
        printf("%d ", a[i]);
    }

    // calling quick_sort() to sort the given array
    quick_sort(a, 0, len - 1);

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

void quick_sort(int b[], int low, int high) {
    if (low < high) {

        // call Partition function to find Partition Index
        int partitionIndex = partition(b, low, high);

        quick_sort(b, low, partitionIndex - 1);
        quick_sort(b, partitionIndex + 1, high);
    }
}

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}


int partition(int b[], int low, int high) {
    int pivot = b[low];
    int i = low;
    int j = high;

    while (i < j) {

        // condition 1: find the first element greater than
        // the pivot (from starting)
        while (b[i] <= pivot && i <= high - 1) {
            i++;
        }

        // condition 2: find the first element smaller than
        // the pivot (from last)
        while (b[j] > pivot && j >= low + 1) {
            j--;
        }
        if (i < j) {
            swap(&b[i], &b[j]);
        }
    }
    swap(&b[low], &b[j]);
    return j; 
}