Quicksort sorts an array by partition the array into two subarrays. It calls itself twice recursively to sort the two resulting subarrays. Inside the partition method, a pivot is the key value to determine when to swap unordered elements. This method returns the index of the partition, the elements at left subarray are less than or equal to the pivot, and the elements at right are greater than or equal to the pivot. The partition marks the boundary between the subarrays.

Quicksort uses divide and conquer algorithm. It operates in O(nlogn) time. It is faster than simple sorting such as bubble sort, selection sort and insertion sort.

There are different ways to get pivot, for example, it can be the first, last or the center item in the subarray. In this solution, it is the center of the array. The formula of its index is mid=low+(high-low)/2. Please notice the differences in how to get the middle index in Java, JavaScript and Python respectively.

Java

Javascript

Python

Doodle

quick sort doodle

Download Java, JavaScript and Python code
Quicksort animated visual