Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Quick Sort

Medium Acceptance 55.23% Points 30

Implement the Quick Sort algorithm, which is a Divide and Conquer technique, to sort an array arr[] in ascending order. Given an array arr[], with a starting index low and an ending index high, complete the functions partition() and quickSort(). The pivot should be chosen as the last element of the array, such that all elements smaller than or equal to the pivot are placed before it, and all elements greater than the pivot are placed after it.

Note: The indices low and high are inclusive.

Examples
Example 1

Example 1

Input: arr[] = [4, 1, 3, 9, 7]

Output: [1, 3, 4, 7, 9]

Explanation: After sorting, all elements are arranged in ascending order.

Example 2

Example 2

Input: arr[] = [2, 1, 6, 10, 4, 1, 3, 9, 7]

Output: [1, 1, 2, 3, 4, 6, 7, 9, 10]

Explanation: Duplicate elements (1) are retained in sorted order.

Constraints
  • 1 <= arr.size() <= 10^5
  • 1 <= arr[i] <= 10^5
Hints
  • Expected Time Complexity: O(n log n)
  • Expected Auxiliary Space: O(log n)
Topics
Sorting Algorithms
Companies
Amazon Microsoft Samsung Adobe Goldman Sachs Qualcomm
Added 30 Dec 2024 55.23% acceptance
Ln 1, Col 1Sign in to save your code
4 1 3 9 7
1 3 4 7 9