-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick.py
More file actions
76 lines (54 loc) · 2.03 KB
/
quick.py
File metadata and controls
76 lines (54 loc) · 2.03 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from typing import List, Union
def quick_sort(arr: List[Union[int, float]]) -> None:
"""
Sorts a list in ascending order using the quick sort algorithm.
Args:
arr (List[Union[int, float]]): The list of numbers (integers or floats) to be sorted.
Returns:
None: The function sorts the list in place and does not return a value.
Example:
>>> nums = [1, 34, 21, 1, 5, 3, 54, 9]
>>> quick_sort(nums)
>>> print(nums)
[1, 1, 3, 5, 9, 21, 34, 54]
"""
perform_quick_sort(arr, 0, len(arr) - 1)
def perform_quick_sort(arr: List[Union[int, float]], low: int, high: int) -> None:
"""
Recursively sorts the sub-array using the quick sort algorithm.
Args:
arr (List[Union[int, float]]): The list of elements to be sorted.
low (int): The starting index of the sub-array to be sorted.
high (int): The ending index of the sub-array to be sorted.
"""
if low < high:
pivot_index = partition_by_pivot(arr, low, high)
perform_quick_sort(arr, low, pivot_index - 1)
perform_quick_sort(arr, pivot_index + 1, high)
def partition_by_pivot(arr: List[Union[int, float]], low: int, high: int) -> int:
"""
Partitions the array around a pivot element and returns the pivot index.
Args:
arr (List[Union[int, float]]): The list of elements to be partitioned.
low (int): The starting index of the sub-array to be partitioned.
high (int): The ending index of the sub-array to be partitioned.
Returns:
int: The index of the pivot element after partitioning.
"""
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
# Test arrays
array_one = [1, 34, 21, 1, 5, 3, 54, 9]
array_two = [4.5, 3.7, 1.6, 0.4, 23.87]
# Sort arrays
quick_sort(array_one)
quick_sort(array_two)
# Print sorted arrays
print(array_one)
print(array_two)