Skip to content
Closed
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
18 changes: 16 additions & 2 deletions sorts/cocktail_shaker_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
https://en.wikipedia.org/wiki/Cocktail_shaker_sort
"""

from typing import Protocol

def cocktail_shaker_sort(arr: list[int]) -> list[int]:

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





def cocktail_shaker_sort[T: Comparable](arr: list[T]) -> list[T]:
"""
Sorts a list using the Cocktail Shaker Sort algorithm.

Expand All @@ -28,7 +37,12 @@
Traceback (most recent call last):
...
TypeError: 'tuple' object does not support item assignment
"""

Check failure on line 40 in sorts/cocktail_shaker_sort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W293)

sorts/cocktail_shaker_sort.py:40:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
>>> cocktail_shaker_sort(["elderberry", "banana", "date", "apple", "cherry"])
['apple', 'banana', 'cherry', 'date', 'elderberry']
>>> cocktail_shaker_sort([3.2, -1.1, 2.4, 0.5])
[-1.1, 0.5, 2.4, 3.2]
"""
start, end = 0, len(arr) - 1

while start < end:
Expand Down
18 changes: 16 additions & 2 deletions sorts/comb_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@
python comb_sort.py
"""

from typing import Protocol

def comb_sort(data: list) -> list:

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





def comb_sort[T: Comparable](data: list[T]) -> list[T]:
"""Pure implementation of comb sort algorithm in Python
:param data: mutable collection with comparable items
:return: the same collection in ascending order
Expand All @@ -32,7 +41,12 @@
[-15, -7, 0, 2, 3, 8, 45, 99]
>>> comb_sort([2, 0, 3, 4, 5, 6, 1])
[0, 1, 2, 3, 4, 5, 6]
"""

Check failure on line 44 in sorts/comb_sort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W293)

sorts/comb_sort.py:44:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
>>> comb_sort(["d", "a", "c", "b"])
['a', 'b', 'c', 'd']
>>> comb_sort([2.5, -1.0, 0.0])
[-1.0, 0.0, 2.5]
"""
shrink_factor = 1.3
gap = len(data)
completed = False
Expand Down
18 changes: 16 additions & 2 deletions sorts/cycle_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
Source: https://en.wikipedia.org/wiki/Cycle_sort
"""

from typing import Protocol

def cycle_sort(array: list) -> list:

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





def cycle_sort[T: Comparable](array: list[T]) -> list[T]:
"""
>>> cycle_sort([4, 3, 2, 1])
[1, 2, 3, 4]
Expand All @@ -17,7 +26,12 @@

>>> cycle_sort([])
[]
"""

Check failure on line 29 in sorts/cycle_sort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W293)

sorts/cycle_sort.py:29:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
>>> cycle_sort(["d", "a", "c", "b"])
['a', 'b', 'c', 'd']
>>> cycle_sort([2.5, -1.0, 0.0])
[-1.0, 0.0, 2.5]
"""
array_len = len(array)
for cycle_start in range(array_len - 1):
item = array[cycle_start]
Expand Down
98 changes: 54 additions & 44 deletions sorts/double_sort.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
from typing import Any


def double_sort(collection: list[Any]) -> list[Any]:
"""This sorting algorithm sorts an array using the principle of bubble sort,
but does it both from left to right and right to left.
Hence, it's called "Double sort"
:param collection: mutable ordered sequence of elements
:return: the same collection in ascending order
Examples:
>>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6 ,-7])
[-7, -6, -5, -4, -3, -2, -1]
>>> double_sort([])
[]
>>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6])
[-6, -5, -4, -3, -2, -1]
>>> double_sort([-3, 10, 16, -42, 29]) == sorted([-3, 10, 16, -42, 29])
True
"""
no_of_elements = len(collection)
for _ in range(
int(((no_of_elements - 1) / 2) + 1)
): # we don't need to traverse to end of list as
for j in range(no_of_elements - 1):
# apply the bubble sort algorithm from left to right (or forwards)
if collection[j + 1] < collection[j]:
collection[j], collection[j + 1] = collection[j + 1], collection[j]
# apply the bubble sort algorithm from right to left (or backwards)
if collection[no_of_elements - 1 - j] < collection[no_of_elements - 2 - j]:
(
collection[no_of_elements - 1 - j],
collection[no_of_elements - 2 - j],
) = (
collection[no_of_elements - 2 - j],
collection[no_of_elements - 1 - j],
)
return collection


if __name__ == "__main__":
# allow the user to input the elements of the list on one line
unsorted = [int(x) for x in input("Enter the list to be sorted: ").split() if x]
print("the sorted list is")
print(f"{double_sort(unsorted) = }")

from typing import Protocol


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


def double_sort[T: Comparable](collection: list[T]) -> list[T]:
"""This sorting algorithm sorts an array using the principle of bubble sort,
but does it both from left to right and right to left.
Hence, it's called "Double sort"
:param collection: mutable ordered sequence of elements
:return: the same collection in ascending order
Examples:
>>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6 ,-7])
[-7, -6, -5, -4, -3, -2, -1]
>>> double_sort([])
[]
>>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6])
[-6, -5, -4, -3, -2, -1]
>>> double_sort([-3, 10, 16, -42, 29]) == sorted([-3, 10, 16, -42, 29])
True

Check failure on line 24 in sorts/double_sort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W293)

sorts/double_sort.py:24:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
>>> double_sort(["d", "a", "c", "b"])
['a', 'b', 'c', 'd']
>>> double_sort([2.5, -1.0, 0.0])
[-1.0, 0.0, 2.5]
"""
no_of_elements = len(collection)
for _ in range(
int(((no_of_elements - 1) / 2) + 1)
): # we don't need to traverse to end of list as
for j in range(no_of_elements - 1):
# apply the bubble sort algorithm from left to right (or forwards)
if collection[j + 1] < collection[j]:
collection[j], collection[j + 1] = collection[j + 1], collection[j]
# apply the bubble sort algorithm from right to left (or backwards)
if collection[no_of_elements - 1 - j] < collection[no_of_elements - 2 - j]:
(
collection[no_of_elements - 1 - j],
collection[no_of_elements - 2 - j],
) = (
collection[no_of_elements - 2 - j],
collection[no_of_elements - 1 - j],
)
return collection


if __name__ == "__main__":
# allow the user to input the elements of the list on one line
unsorted = [int(x) for x in input("Enter the list to be sorted: ").split() if x]
print("the sorted list is")
print(f"{double_sort(unsorted) = }")
16 changes: 14 additions & 2 deletions sorts/exchange_sort.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
def exchange_sort(numbers: list[int]) -> list[int]:
from typing import Protocol


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


def exchange_sort[T: Comparable](numbers: list[T]) -> list[T]:
"""
Uses exchange sort to sort a list of numbers.
Source: https://en.wikipedia.org/wiki/Sorting_algorithm#Exchange_sort
Expand All @@ -12,7 +19,12 @@
[-2, 0, 3, 5, 10]
>>> exchange_sort([])
[]
"""

Check failure on line 22 in sorts/exchange_sort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W293)

sorts/exchange_sort.py:22:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
>>> exchange_sort(["d", "a", "c", "b"])
['a', 'b', 'c', 'd']
>>> exchange_sort([2.5, -1.0, 0.0])
[-1.0, 0.0, 2.5]
"""
numbers_length = len(numbers)
for i in range(numbers_length):
for j in range(i + 1, numbers_length):
Expand Down
18 changes: 16 additions & 2 deletions sorts/gnome_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@
python3 gnome_sort.py
"""

from typing import Protocol

def gnome_sort(lst: list) -> list:

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





def gnome_sort[T: Comparable](lst: list[T]) -> list[T]:
"""
Pure implementation of the gnome sort algorithm in Python

Expand All @@ -32,7 +41,12 @@

>>> "".join(gnome_sort(list(set("Gnomes are stupid!"))))
' !Gadeimnoprstu'
"""

Check failure on line 44 in sorts/gnome_sort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W293)

sorts/gnome_sort.py:44:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
>>> gnome_sort(["d", "a", "c", "b"])
['a', 'b', 'c', 'd']
>>> gnome_sort([2.5, -1.0, 0.0])
[-1.0, 0.0, 2.5]
"""
if len(lst) <= 1:
return lst

Expand Down
18 changes: 18 additions & 0 deletions tests/test_sorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@
on a graph, and ``stalin_sort``/``wiggle_sort`` deliberately do not fully sort).
"""

import pytest

from sorts.binary_insertion_sort import binary_insertion_sort
from sorts.cocktail_shaker_sort import cocktail_shaker_sort
from sorts.comb_sort import comb_sort
from sorts.cycle_sort import cycle_sort
from sorts.double_sort import double_sort
from sorts.exchange_sort import exchange_sort
from sorts.gnome_sort import gnome_sort
from sorts.bubble_sort import bubble_sort_iterative, bubble_sort_recursive
from sorts.circle_sort import circle_sort
from sorts.cocktail_shaker_sort import cocktail_shaker_sort

Check failure on line 28 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F811)

tests/test_sorts.py:28:40: F811 Redefinition of unused `cocktail_shaker_sort` from line 20: `cocktail_shaker_sort` redefined here tests/test_sorts.py:20:40: previous definition of `cocktail_shaker_sort` here help: Remove definition: `cocktail_shaker_sort`
from sorts.comb_sort import comb_sort

Check failure on line 29 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F811)

tests/test_sorts.py:29:29: F811 Redefinition of unused `comb_sort` from line 21: `comb_sort` redefined here tests/test_sorts.py:21:29: previous definition of `comb_sort` here help: Remove definition: `comb_sort`
from sorts.cycle_sort import cycle_sort

Check failure on line 30 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F811)

tests/test_sorts.py:30:30: F811 Redefinition of unused `cycle_sort` from line 22: `cycle_sort` redefined here tests/test_sorts.py:22:30: previous definition of `cycle_sort` here help: Remove definition: `cycle_sort`
from sorts.double_sort import double_sort
from sorts.exchange_sort import exchange_sort
from sorts.gnome_sort import gnome_sort
Expand All @@ -48,6 +54,12 @@

SORTS = (
binary_insertion_sort,
cocktail_shaker_sort,
comb_sort,
cycle_sort,
double_sort,
exchange_sort,
gnome_sort,
bubble_sort_iterative,
circle_sort,
cocktail_shaker_sort,
Expand Down Expand Up @@ -95,6 +107,12 @@
binary_insertion_sort,
bubble_sort_iterative,
bubble_sort_recursive,
cocktail_shaker_sort,
comb_sort,
cycle_sort,
double_sort,
exchange_sort,
gnome_sort,
insertion_sort,
],
ids=lambda f: f.__name__,
Expand Down
Loading