-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbubble_sort.py
37 lines (32 loc) · 992 Bytes
/
bubble_sort.py
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
"""
https://en.wikipedia.org/wiki/Bubble_sort
"""
def bubble_sort(array):
"""
:param array: the array to be sorted.
:return: sorted array.
>>> import random
>>> array = random.sample(range(-50, 50), 100)
>>> bubble_sort(array) == sorted(array)
True
>>> import string
>>> array = random.choices(string.ascii_letters + string.digits, k = 100)
>>> bubble_sort(array) == sorted(array)
True
>>> array = [random.uniform(-50.0, 50.0) for i in range(100)]
>>> bubble_sort(array) == sorted(array)
True
"""
length = len(array)
for i in range(0, length - 1):
swapped = False
for j in range(0, length - 1 - i):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
swapped = True
if not swapped:
break
return array
if __name__ == "__main__":
from doctest import testmod
testmod()