mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
07e991d553
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
"""
|
|
Picks the random index as the pivot
|
|
"""
|
|
import random
|
|
|
|
|
|
def partition(a, left_index, right_index):
|
|
pivot = a[left_index]
|
|
i = left_index + 1
|
|
for j in range(left_index + 1, right_index):
|
|
if a[j] < pivot:
|
|
a[j], a[i] = a[i], a[j]
|
|
i += 1
|
|
a[left_index], a[i - 1] = a[i - 1], a[left_index]
|
|
return i - 1
|
|
|
|
|
|
def quick_sort_random(a, left, right):
|
|
if left < right:
|
|
pivot = random.randint(left, right - 1)
|
|
a[pivot], a[left] = (
|
|
a[left],
|
|
a[pivot],
|
|
) # switches the pivot with the left most bound
|
|
pivot_index = partition(a, left, right)
|
|
quick_sort_random(
|
|
a, left, pivot_index
|
|
) # recursive quicksort to the left of the pivot point
|
|
quick_sort_random(
|
|
a, pivot_index + 1, right
|
|
) # recursive quicksort to the right of the pivot point
|
|
|
|
|
|
def main():
|
|
user_input = input("Enter numbers separated by a comma:\n").strip()
|
|
arr = [int(item) for item in user_input.split(",")]
|
|
|
|
quick_sort_random(arr, 0, len(arr))
|
|
|
|
print(arr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|