Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Quick Sort

Medium Acceptance 55.23% Points 30.00

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.

Hints
Hint 1
Expected Time Complexity: O(n log n)
Hint 2
Expected Auxiliary Space: O(log n)
Constraints
  • 1 <= arr.size() <= 10^5
  • 1 <= arr[i] <= 10^5
Companies
Amazon Microsoft Samsung Adobe Goldman Sachs + 1 more
Topics
Sorting Algorithms
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions