-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathbubble.py
35 lines (27 loc) · 929 Bytes
/
bubble.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
""" Implementation of Bubble Sort algorithm
"""
def bubble(array):
""" Bubble sort is a simple sorting algorithm that repeatedly steps
through the list to be sorted, compares each pair of adjacent items and
swaps them if they are in the wrong order. The pass through the list is
repeated until no swaps are needed, which indicates that the list is
sorted.
:param array: list of elements that needs to be sorted
:type array: list
"""
length = len(array)
swapped = True
while swapped:
swapped = False
for i in range(length - 1):
if array[i] > array[i+1]:
array[i], array[i+1] = array[i+1], array[i]
swapped = True
def main():
""" operational function """
arr = [34, 56, 23, 67, 3, 68]
print(f"unsorted array: {arr}")
bubble(arr)
print(f" sorted array: {arr}")
if __name__ == "__main__":
main()