Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 56 additions & 11 deletions sorts/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
from typing import Any


def bubble_sort(collection: list[Any]) -> list[Any]:
def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
"""Pure implementation of bubble sort algorithm in Python

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

Examples:
>>> bubble_sort([0, 5, 2, 3, 2])
>>> bubble_sort_iterative([0, 5, 2, 3, 2])
[0, 2, 2, 3, 5]
>>> bubble_sort([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
>>> bubble_sort_iterative([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
True
>>> bubble_sort([]) == sorted([])
>>> bubble_sort_iterative([]) == sorted([])
True
>>> bubble_sort([-2, -45, -5]) == sorted([-2, -45, -5])
>>> bubble_sort_iterative([-2, -45, -5]) == sorted([-2, -45, -5])
True
>>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
>>> bubble_sort_iterative([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
True
>>> bubble_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c'])
>>> bubble_sort_iterative(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
True
>>> import random
>>> collection = random.sample(range(-50, 50), 100)
>>> bubble_sort(collection) == sorted(collection)
>>> bubble_sort_iterative(collection) == sorted(collection)
True
>>> import string
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
>>> bubble_sort(collection) == sorted(collection)
>>> bubble_sort_iterative(collection) == sorted(collection)
True
"""
length = len(collection)
Expand All @@ -42,6 +42,44 @@ def bubble_sort(collection: list[Any]) -> list[Any]:
return collection


def bubble_sort_recursive(list_data: list, length: int = 0) -> list:
"""
It is similar is bubble sort but recursive.
:param list_data: mutable ordered sequence of elements
:param length: length of list data
:return: the same list in ascending order

>>> bubble_sort_recursive([0, 5, 2, 3, 2], 5)
[0, 2, 2, 3, 5]

>>> bubble_sort_recursive([], 0)
[]

>>> bubble_sort_recursive([-2, -45, -5], 3)
[-45, -5, -2]

>>> bubble_sort_recursive([-23, 0, 6, -4, 34], 5)
[-23, -4, 0, 6, 34]

>>> bubble_sort_recursive([-23, 0, 6, -4, 34], 5) == sorted([-23, 0, 6, -4, 34])
True

>>> bubble_sort_recursive(['z','a','y','b','x','c'], 6)
['a', 'b', 'c', 'x', 'y', 'z']

>>> bubble_sort_recursive([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
"""
length = length or len(list_data)
swapped = False
for i in range(length - 1):
if list_data[i] > list_data[i + 1]:
list_data[i], list_data[i + 1] = list_data[i + 1], list_data[i]
swapped = True

return list_data if not swapped else bubble_sort_recursive(list_data, length - 1)


if __name__ == "__main__":
import doctest
import time
Expand All @@ -50,6 +88,13 @@ def bubble_sort(collection: list[Any]) -> list[Any]:

user_input = input("Enter numbers separated by a comma:").strip()
unsorted = [int(item) for item in user_input.split(",")]

print("Iterative bubble sort:")
start = time.process_time()
print(*bubble_sort_iterative(unsorted), sep=",")
print(f"Processing time (iterative): {(time.process_time() - start) % 1e9 + 7}")

print("\nRecursive bubble sort:")
start = time.process_time()
print(*bubble_sort(unsorted), sep=",")
print(f"Processing time: {(time.process_time() - start)%1e9 + 7}")
print(bubble_sort_recursive(unsorted, len(unsorted)))
print(f"Processing time (recursive): {(time.process_time() - start) % 1e9 + 7}")
42 changes: 0 additions & 42 deletions sorts/recursive_bubble_sort.py

This file was deleted.