Skip to content

Create quicksort.py #12048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions divide_and_conquer/quicksort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from __future__ import annotations

def partition(array: list, low: int, high: int) -> int:

Check failure on line 3 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

divide_and_conquer/quicksort.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 3 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

divide_and_conquer/quicksort.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 3 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

divide_and_conquer/quicksort.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 3 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

divide_and_conquer/quicksort.py:1:1: I001 Import block is un-sorted or un-formatted
"""Helper function for quicksort to partition the array.

>>> array = [3, 2, 1]
>>> partition(array, 0, len(array) - 1)
0
>>> array
[1, 2, 3]

>>> array = [12, 4, 5, 2, 3]
>>> idx = partition(array, 0, len(array) - 1)
>>> array[:idx], array[idx], array[idx+1:]
([2, 3, 4], 5, [12])
"""
pivot = array[high]
i = low - 1 # Pointer for the smaller element

for j in range(low, high):
if array[j] <= pivot:
i += 1
array[i], array[j] = array[j], array[i]

Check failure on line 24 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:24:1: W293 Blank line contains whitespace

Check failure on line 24 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:24:1: W293 Blank line contains whitespace

Check failure on line 24 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:24:1: W293 Blank line contains whitespace

Check failure on line 24 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:24:1: W293 Blank line contains whitespace
array[i + 1], array[high] = array[high], array[i + 1]
return i + 1

def quicksort(array: list, low: int = 0, high: int | None = None) -> list:
"""Returns a sorted list using the quicksort algorithm.

>>> from random import shuffle
>>> array = [-2, 3, -10, 11, 99, 100000, 100, -200]
>>> shuffle(array)
>>> quicksort(array)
[-200, -10, -2, 3, 11, 99, 100, 100000]

>>> shuffle(array)
>>> quicksort(array)
[-200, -10, -2, 3, 11, 99, 100, 100000]

>>> quicksort([-200])
[-200]

>>> array = [-2, 3, -10, 11, 99, 100000, 100, -200]
>>> shuffle(array)
>>> sorted(array) == quicksort(array)
True

>>> quicksort([])
[]

>>> array = [10000000, 1, -1111111111, 101111111112, 9000002]
>>> sorted(array) == quicksort(array)
True
"""
if high is None:
high = len(array) - 1

Check failure on line 58 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:58:1: W293 Blank line contains whitespace

Check failure on line 58 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:58:1: W293 Blank line contains whitespace

Check failure on line 58 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:58:1: W293 Blank line contains whitespace

Check failure on line 58 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:58:1: W293 Blank line contains whitespace
if low < high:
pivot_index = partition(array, low, high)
quicksort(array, low, pivot_index - 1)
quicksort(array, pivot_index + 1, high)

Check failure on line 63 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:63:1: W293 Blank line contains whitespace

Check failure on line 63 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:63:1: W293 Blank line contains whitespace

Check failure on line 63 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:63:1: W293 Blank line contains whitespace

Check failure on line 63 in divide_and_conquer/quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

divide_and_conquer/quicksort.py:63:1: W293 Blank line contains whitespace
return array

if __name__ == "__main__":
import doctest
doctest.testmod()
Loading