quick sort algorithm

Quicksort is a divide-and-conquer algorithm. In this method, a 'pivot' element is selected from the array and is partitioned into subarrays, the elements less than the pivot are placed to the left of the pivot, and elements greater than the pivot are placed to the right of the pivot. The sub-arrays are then sorted recursively.    


                     sorted array => {-6,-3,1,2,3,5,6,8,9}


quickSort(arr[], low, high)
{
    if (low < high)
    {
         int pi= partition(arr, low, high);

        quickSort(arr, pi, pi- 1);  // Before pi
        quickSort(arr, pi + 1, high); // After pi
    }
}


partition (arr[], low, high)
{
    int pivot = arr[high];
    int i = low; 
  
    for (int j = low; j <high ; j++) 
    { 
        if (arr[j] < pivot) 
        { 
            swap(arr[i], arr[j]); 
            i++;
        } 
    } 
    swap(arr[i], arr[high]); 
    return i;
}







darkmode