mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-30 18:36:43 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
7417932ba4
commit
7299b9c866
@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def partition(array: list, start: int, end: int) -> int:
|
||||
"""Helper function for Quick Sort.
|
||||
|
||||
@ -22,7 +23,7 @@ def partition(array: list, start: int, end: int) -> int:
|
||||
>>> array
|
||||
[]
|
||||
"""
|
||||
if not array:
|
||||
if not array:
|
||||
return 0
|
||||
pivot = array[end]
|
||||
index = start - 1
|
||||
@ -30,8 +31,9 @@ def partition(array: list, start: int, end: int) -> int:
|
||||
if array[j] <= pivot:
|
||||
index += 1
|
||||
array[index], array[j] = array[j], array[index]
|
||||
array[index+1], array[end] = array[end], array[index+1]
|
||||
return index+1
|
||||
array[index + 1], array[end] = array[end], array[index + 1]
|
||||
return index + 1
|
||||
|
||||
|
||||
def quicksort(array, start, end):
|
||||
"""Returns a list of sorted array elements using Quick Sort.
|
||||
@ -61,9 +63,11 @@ def quicksort(array, start, end):
|
||||
"""
|
||||
if start < end:
|
||||
partition_index = partition(array, start, end)
|
||||
quicksort(array, start, partition_index-1)
|
||||
quicksort(array, partition_index+1, end)
|
||||
quicksort(array, start, partition_index - 1)
|
||||
quicksort(array, partition_index + 1, end)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
Loading…
x
Reference in New Issue
Block a user