def partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
def quick_sort(arr, low, high):
if low < high:
pivot_index = partition(arr, low, high)
quick_sort(arr, low, pivot_index - 1)
quick_sort(arr, pivot_index + 1, high)
# Example usage
arr = [64, 34, 25, 12, 22, 11, 90]
quick_sort(arr, 0, len(arr) - 1)
print(arr)
[11, 12, 22, 25, 34, 64, 90]
Quick Sort is an efficient sorting algorithm with an average time complexity of O(n log n). It works by selecting a pivot element and partitioning the array around the pivot, recursively sorting the subarrays before and after the pivot. The choice of pivot can affect the performance of the algorithm in certain cases, such as when the array is already sorted or contains many duplicate elements.