Quick Sort

Quick Sort is an efficient sorting algorithm that uses a divide-and-conquer approach to sort elements. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.

#include<stdio.h>
void quickSort(int[], int, int);
int main(){
    int n, arr[50];
    printf("Enter the number of elements: ");
    scanf("%d",&n);
    printf("Enter %d elements: ",n);
    for(int i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    quickSort(arr, 0, n-1);
    printf("Sorted array: ");
    for(int i=0;i<n;i++){
        printf("%d ",arr[i]);
    }
    return 0;
}

void quickSort(int arr[], int low, int high){
    int i = low, j = high, temp, pivot = arr[(low+high)/2];
    while(i<=j){
        while(arr[i]<pivot){
            i++;
        }
        while(arr[j]>pivot){
            j--;
        }
        if(i<=j){
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
    }
    if(low<j){
        quickSort(arr, low, j);
    }
    if(i<high){
        quickSort(arr, i, high);
    }
}
Sample Input and Output:
Enter the number of elements: 7
Enter 7 elements: 6 3 12 9 15 21 18
Sorted array: 3 6 9 12 15 18 21