write a quick sort in py
- User
write a quick sort in py
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write a quick sort in py
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Of course. Here are two common ways to write Quick Sort in Python.
This version is elegant and easy to understand. It uses list comprehensions to partition the array, which clearly demonstrates the "divide and conquer" logic. However, creating new lists for each recursive call makes it less space-efficient than the in-place version.
def quick_sort(arr):
"""
A simple, elegant, and Pythonic implementation of Quick Sort.
Note: This is not in-place and uses O(n) space complexity on average
due to the creation of new lists in each recursive call.
"""
# Base case: arrays with 0 or 1 element are already sorted
if len(arr) <= 1:
return arr
else:
# Choose the pivot (here, we take the middle element)
pivot = arr[len(arr) // 2]
# Partition the array into three parts
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
# Recursively sort the left and right parts and combine them
return quick_sort(left) + middle + quick_sort(right)
# --- Example Usage ---
my_list = [3, 6, 8, 10, 1, 2, 1]
sorted_list = quick_sort(my_list)
print("Original list:", my_list)
print("Sorted list: ", sorted_list)
# Output:
# Original list: [3, 6, 8, 10, 1, 2, 1]
# Sorted list: [1, 1, 2, 3, 6, 8, 10]
This is the more traditional and memory-efficient implementation. It sorts the array by modifying it directly ("in-place") without creating new copies of sub-arrays. This version uses O(log n) space for the recursion call stack.
It works by partitioning the array around a pivot and then recursively sorting the sub-arrays to the left and right of the pivot.
def partition(arr, low, high):
"""
This function takes last element as pivot, places the pivot element at its
correct position in sorted array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right of pivot.
"""
# Choose the rightmost element as pivot
pivot = arr[high]
# Pointer for the greater element
i = low - 1
# Traverse through all elements and compare them with pivot
for j in range(low, high):
if arr[j] <= pivot:
# If element smaller than pivot is found, swap it with the greater element pointed by i
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
# Swap the pivot element with the greater element specified by i
arr[i + 1], arr[high] = arr[high], arr[i + 1]
# Return the position from where partition is done
return i + 1
def quick_sort_inplace(arr, low, high):
"""
The main function to implement Quick Sort, which sorts the array in-place.
"""
if low < high:
# Find pivot element such that element smaller than pivot are on the left
# and elements greater than pivot are on the right
pi = partition(arr, low, high)
# Recursive call on the left of pivot
quick_sort_inplace(arr, low, pi - 1)
# Recursive call on the right of pivot
quick_sort_inplace(arr, pi + 1, high)
# --- Example Usage ---
my_list2 = [10, 7, 8, 9, 1, 5]
n = len(my_list2)
print("Original list:", my_list2)
# The function modifies the list in place, so no return value is needed.
quick_sort_inplace(my_list2, 0, n - 1)
print("Sorted list: ", my_list2)
# Output:
# Original list: [10, 7, 8, 9, 1, 5]
# Sorted list: [1, 5, 7, 8, 9, 10]