Skip to content
Open
Show file tree
Hide file tree
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
26 changes: 19 additions & 7 deletions sorts/bogo_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,39 @@
"""

import random
from typing import Any, Protocol


def bogo_sort(collection: list) -> list:
"""Pure implementation of the bogosort algorithm in Python
class Comparable(Protocol):
def __lt__(self, other: Any, /) -> bool: ...


def bogo_sort[T: Comparable](collection: list[T]) -> list[T]:
"""Pure implementation of the bogosort algorithm in Python.

:param collection: some mutable ordered collection with heterogeneous
comparable items inside
:return: the same collection ordered by ascending

Examples:
>>> bogo_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> bogo_sort([])
[]
>>> bogo_sort([-2, -5, -45])
[-45, -5, -2]
>>> bogo_sort(["c", "a", "b"])
['a', 'b', 'c']
>>> bogo_sort([2.5, -1.0, 0.0])
[-1.0, 0.0, 2.5]
>>> bogo_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2])
True
"""

def is_sorted(collection: list) -> bool:
for i in range(len(collection) - 1):
if collection[i] > collection[i + 1]:
return False
return True
def is_sorted(collection: list[T]) -> bool:
return all(
collection[i] <= collection[i + 1] for i in range(len(collection) - 1)
)

while not is_sorted(collection):
random.shuffle(collection)
Expand Down
32 changes: 18 additions & 14 deletions sorts/quick_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
from __future__ import annotations

from random import randrange
from typing import Any, Protocol


def quick_sort(collection: list) -> list:
class Comparable(Protocol):
def __lt__(self, other: Any, /) -> bool: ...


def quick_sort[T: Comparable](collection: list[T]) -> list[T]:
"""A pure Python implementation of quicksort algorithm.

:param collection: a mutable collection of comparable items
Expand All @@ -26,27 +31,26 @@ def quick_sort(collection: list) -> list:
[]
>>> quick_sort([-2, 5, 0, -45])
[-45, -2, 0, 5]
>>> quick_sort(["z", "a", "m", "b"])
['a', 'b', 'm', 'z']
>>> quick_sort([3.14, -1.0, 2.71])
[-1.0, 2.71, 3.14]
>>> quick_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2])
True
>>> quick_sort(["z", "a", "m"]) == sorted(["z", "a", "m"])
True
"""
# Base case: if the collection has 0 or 1 elements, it is already sorted
if len(collection) < 2:
return collection

# Randomly select a pivot index and remove the pivot element from the collection
pivot_index = randrange(len(collection))
pivot = collection.pop(pivot_index)

# Partition the remaining elements into two groups: lesser or equal, and greater
lesser = [item for item in collection if item <= pivot]
pivot = collection[pivot_index]
lesser = [item for item in collection if item < pivot]
equal = [item for item in collection if item == pivot]
greater = [item for item in collection if item > pivot]

# Recursively sort the lesser and greater groups, and combine with the pivot
return [*quick_sort(lesser), pivot, *quick_sort(greater)]
return [*quick_sort(lesser), *equal, *quick_sort(greater)]


if __name__ == "__main__":
# Get user input and convert it into a list of integers
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]

# Print the result of sorting the user-provided list
print(quick_sort(unsorted))
23 changes: 19 additions & 4 deletions sorts/shell_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@
https://en.wikipedia.org/wiki/Shellsort#Pseudocode
"""

from typing import Any, Protocol


class Comparable(Protocol):
def __lt__(self, other: Any, /) -> bool: ...


def shell_sort[T: Comparable](collection: list[T]) -> list[T]:
"""Pure implementation of shell sort algorithm in Python.

def shell_sort(collection: list[int]) -> list[int]:
"""Pure implementation of shell sort algorithm in Python
:param collection: Some mutable ordered collection with heterogeneous
comparable items inside
:return: the same collection ordered by ascending

Examples:
>>> shell_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> shell_sort([])
[]
>>> shell_sort([-2, -5, -45])
[-45, -5, -2]
>>> shell_sort(["c", "a", "b"])
['a', 'b', 'c']
>>> shell_sort([2.5, -1.0, 0.0])
[-1.0, 0.0, 2.5]
>>> shell_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2])
True
>>> shell_sort(["c", "a", "b"]) == sorted(["c", "a", "b"])
True
"""
# Marcin Ciura's gap sequence

gaps = [701, 301, 132, 57, 23, 10, 4, 1]
for gap in gaps:
for i in range(gap, len(collection)):
insert_value = collection[i]
j = i
while j >= gap and collection[j - gap] > insert_value:
while j >= gap and insert_value < collection[j - gap]:
collection[j] = collection[j - gap]
j -= gap
if j != i:
Expand Down
Loading