-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathbubble_sort.py
More file actions
64 lines (50 loc) · 1.78 KB
/
bubble_sort.py
File metadata and controls
64 lines (50 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Bubble Sort Algorithm.
This module implements the bubble sort algorithm, a simple sorting technique
that repeatedly steps through the list, compares adjacent elements and swaps
them if they are in the wrong order.
"""
def bubble_sort(arr: list[int | float]) -> list[int | float]:
"""
Sort an array using the bubble sort algorithm.
Bubble sort works by repeatedly swapping adjacent elements if they are
in the wrong order. This process continues until no more swaps are needed.
Args:
arr: List of numbers to be sorted
Returns:
The sorted list in ascending order
Examples:
>>> bubble_sort([64, 34, 25, 12, 22, 11, 90])
[11, 12, 22, 25, 34, 64, 90]
>>> bubble_sort([5, 2, 8, 1, 9])
[1, 2, 5, 8, 9]
>>> bubble_sort([1])
[1]
>>> bubble_sort([])
[]
>>> bubble_sort([3, 3, 3, 3])
[3, 3, 3, 3]
>>> bubble_sort([5, 4, 3, 2, 1])
[1, 2, 3, 4, 5]
>>> bubble_sort([1.5, 2.3, 0.8, 1.1])
[0.8, 1.1, 1.5, 2.3]
"""
# Create a copy to avoid modifying the original list
arr_copy = arr.copy()
n = len(arr_copy)
# Traverse through all array elements
for i in range(n):
# Flag to optimize by detecting if array is already sorted
swapped = False
# Last i elements are already in place
for j in range(n - i - 1):
# Swap if the element found is greater than the next element
if arr_copy[j] > arr_copy[j + 1]:
arr_copy[j], arr_copy[j + 1] = arr_copy[j + 1], arr_copy[j]
swapped = True
# If no swapping occurred, array is sorted
if not swapped:
break
return arr_copy
if __name__ == "__main__":
import doctest
doctest.testmod()