Tutorialspoint
Problem
Solution
Submissions

Quick Sort Algorithm

Certification: Intermediate Level Accuracy: 100% Submissions: 2 Points: 15

Write a Python program that implements the Quick Sort algorithm to sort a list of integers in ascending order. Quick Sort is a divide-and-conquer algorithm that works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays according to whether they are less than or greater than the pivot.

Example 1
  • Input: [10, 7, 8, 9, 1, 5]
  • Output: [1, 5, 7, 8, 9, 10]
  • Explanation:
    • Step 1: Take the input array [10, 7, 8, 9, 1, 5].
    • Step 2: Choose a pivot (e.g., 10) and partition the array.
    • Step 3: After partitioning: [7, 8, 9, 1, 5] and [] with pivot 10.
    • Step 4: Recursively apply Quick Sort to the sub-arrays.
    • Step 5: Combine the results: [1, 5, 7, 8, 9, 10].
    • Step 6: Return the sorted array [1, 5, 7, 8, 9, 10].
Example 2
  • Input: [64, 34, 25, 12, 22, 11, 90]
  • Output: [11, 12, 22, 25, 34, 64, 90]
  • Explanation:
    • Step 1: Take the input array [64, 34, 25, 12, 22, 11, 90].
    • Step 2: Choose a pivot (e.g., 64) and partition the array.
    • Step 3: After partitioning: [34, 25, 12, 22, 11] and [90] with pivot 64.
    • Step 4: Recursively apply Quick Sort to the sub-arrays.
    • Step 5: Combine the results: [11, 12, 22, 25, 34, 64, 90].
    • Step 6: Return the sorted array [11, 12, 22, 25, 34, 64, 90].
Constraints
  • 1 ≤ len(arr) ≤ 10^5
  • -10^6 ≤ arr[i] ≤ 10^6
  • Time Complexity: O(n log n) average case, O(n²) worst case, where n is the length of the array
  • Space Complexity: O(log n) for recursion stack
ArraysFunctions / MethodsEYDropbox
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Choose a pivot element from the array
  • Partition the array around the pivot such that elements smaller than the pivot are on the left and larger elements are on the right
  • Recursively apply the above steps to the sub-arrays
  • The base case is when a sub-array has one or zero elements
  • Different pivot selection strategies: first element, last element, median of three, random
  • Implement a partition function to rearrange the array around the pivot
  • The recursion depth is log(n) on average, but can be n in the worst case

Steps to solve by this approach:

 Step 1: Create a copy of the input array to avoid modifying the original.
 Step 2: Define a partition function that selects a pivot (rightmost element).
 Step 3: Rearrange the array so elements smaller than pivot are on left, larger on right.
 Step 4: Place the pivot at its correct sorted position and return its index.
 Step 5: Recursively apply quick_sort to the sub-array before the pivot.
 Step 6: Recursively apply quick_sort to the sub-array after the pivot.
 Step 7: Return the completely sorted array.

Submitted Code :