Quicksort Algorithm

Quicksort Algorithm

Quicksort is an algorithm based on divide and conquer approach in which the array is split into subarrays and these sub-arrays are recursively called to sort the elements.

How QuickSort Works?

  1. A pivot element is chosen from the array. You can choose any element from the array as the pviot element.

    Here, we have taken the rightmost (ie. the last element) of the array as the pivot element.
    Quick Sort Steps
  2. The elements smaller than the pivot element are put on the left and the elements greater than the pivot element are put on the right.
    Quick Sort Steps
    The above arrangement is achieved by the following steps.
    1. A pointer is fixed at the pivot element. The pivot element is compared with the elements beginning from the first index. If the element greater than the pivot element is reached, a second pointer is set for that element.
       
    2. Now, the pivot element is compared with the other elements. If element smaller than the pivot element is reached, the smaller element is swapped with the greater element found earlier.
      Quick Sort Steps
    3. The process goes on until the second last element is reached.
       
    4. Finally, the pivot element is swapped with the second pointer.
      Quick Sort Steps
  3. Pivot elements are again chosen for the left and the right sub-parts separately. Within these sub-parts, the pivot elements are placed at their right position. Then, step 2 is repeated.
    Quick Sort Steps
  4. The sub-parts are again divided into smallest sub-parts until each subpart is formed of a single element.
     
  5. At this point, the array is already sorted.

Quicksort uses recursion for sorting the sub-parts.
On the basis of Divide and conquer approach, quicksort algorithm can be explained as:
  • Divide
    The array is divided into subparts taking pivot as the partitioning point. The elements smaller than the pivot are placed to the left of the pivot and the elements greater than the pivot are placed to the right.
  • Conquer
    The left and the right subparts are again partitioned using the by selecting pivot elements for them. This can be achieved by recursively passing the subparts into the algorithm.
  • Combine
    This step does not play a significant role in quicksort. The array is already sorted at the end of the conquer step.
You can understand the working of quicksort with the help of an example/illustration below.
Quick Sort StepsQuick Sort Steps

    Quick Sort Algorithm

    1. quickSort(array, leftmostIndex, rightmostIndex)
    2. if (leftmostIndex < rightmostIndex)
    3. pivotIndex <- partition(array,leftmostIndex, rightmostIndex)
    4. quickSort(array, leftmostIndex, pivotIndex)
    5. quickSort(array, pivotIndex + 1, rightmostIndex)
    6. partition(array, leftmostIndex, rightmostIndex)
    7. set rightmostIndex as pivotIndex
    8. storeIndex <- leftmostIndex - 1
    9. for i <- leftmostIndex + 1 to rightmostIndex
    10. if element[i] < pivotElement
    11. swap element[i] and element[storeIndex]
    12. storeIndex++
    13. swap pivotElement and element[storeIndex+1]
    14. return storeIndex + 1

    C++ Examples

    1. // Quick sort in C++

    1. #include <iostream>
    2. using namespace std;
    3. void swap(int *a, int *b)
    4. {
    5. int t = *a;
    6. *a = *b;
    7. *b = t;
    8. }
    9. void printArray(int array[], int size)
    10. {
    11. int i;
    12. for (i = 0; i < size; i++)
    13. cout << array[i] << " ";
    14. cout << endl;
    15. }
    16. int partition(int array[], int low, int high)
    17. {
    18. int pivot = array[high];
    19. int i = (low - 1);
    20. for (int j = low; j < high; j++)
    21. {
    22. if (array[j] <= pivot)
    23. {
    24. i++;
    25. swap(&array[i], &array[j]);
    26. }
    27. }
    28. printArray(array, 7);
    29. cout << "........\n";
    30. swap(&array[i + 1], &array[high]);
    31. return (i + 1);
    32. }
    33. void quickSort(int array[], int low, int high)
    34. {
    35. if (low < high)
    36. {
    37. int pi = partition(array, low, high);
    38. quickSort(array, low, pi - 1);
    39. quickSort(array, pi + 1, high);
    40. }
    41. }
    42. int main()
    43. {
    44. int data[] = {8, 7, 6, 1, 0, 9, 2};
    45. int n = sizeof(data) / sizeof(data[0]);
    46. quickSort(data, 0, n - 1);
    47. cout << "Sorted array in ascending order: \n";
    48. printArray(data, n);
    49. }
    Time Complexities
    • Worst Case Complexity [Big-O]O(n2) It occurs when the pivot element picked is always either the greatest or the smallest element. In the above algorithm, if the array is in descending order, the partition algorithm always picks the smallest element as a pivot element.  
    • Best Case Complexity [Big-omega]O(n*log n) It occurs when the pivot element is always the middle element or near to the middle element.  
    • Average Case Complexity [Big-theta]O(n*log n) It occurs when the above conditions do not occur.
    Space Complexity The space complexity for quicksort is O(n*log n).

    Quicksort Applications

    Quicksort is implemented when
    • the programming language is good for recursion
    • time complexity matters
    • space complexity matters

    Comments

    Popular posts from this blog

    C Programming Tutorials

    DataStructure and Algorithm

    Java Programming Tutorial