![]() |
VOOZH | about |
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array.
QuickSort works on the principle of divide and conquer, breaking down the problem into smaller sub-problems. There are mainly three steps in the algorithm:
In this implementation, last element is chosen as the pivot, and the partitioning ensures all smaller elements are on one side and larger on the other, achieving sorting efficiently. Let's understand the working of partition algorithm with the help of the following example:
1. Input: Array A, starting index low, ending index high
2. If low < high, then
3. Else: Stop (base case - subarray of size ≤ 1 is already sorted)
4. Output: Sorted array A
This method sorts an array by selecting the last element as a pivot and partitioning the array so that smaller elements move to the left and larger ones to the right. It then recursively applies the same process to the resulting subarrays until the entire array is sorted.
[-2, 1, 1, 4, 7, 9, 10]
Explanation:
This approach uses recursion and list comprehension. It selects the first element as the pivot and divides the array into two lists one for elements smaller than the pivot and one for greater elements and recursively sorts both parts.
[-2, 1, 1, 4, 7, 9, 10]
Explanation: