1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| def quick_sort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort(arr, low, pi - 1)
quick_sort(arr, pi + 1, high)
def partition(arr, low, high):
mid = low + (high - low) // 2
pivot = sorted([arr[low], arr[mid], arr[high]])[1]
pivot_index = arr.index(pivot)
# Move the pivot to the end for the partitioning process
arr[pivot_index], arr[high] = arr[high], arr[pivot_index]
i = low
for j in range(low, high):
if arr[j] <= pivot:
arr[i], arr[j] = arr[j], arr[i] # Swap elements
i += 1
arr[i], arr[high] = arr[high], arr[i] # Swap pivot to the right position
return i
example_array = [10, 7, 8, 9, 1, 5]
quick_sort(example_array, 0, len(example_array) - 1)
print(example_array) # Output : [1, 5, 7, 8, 9, 10]
|