forked from NITSkmOS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort.py
22 lines (17 loc) · 852 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
def bubble_sort(arr): # Function that performs the bubbleSort
"""
The bubble_sort funtion takes an array as an argument
and then sorts the elements present in that array.
:param arr: A array for sorting
"""
n = len(arr) # Finding the length of the array using the len function
for i in range(n): # Traverse through all array elements
for j in range(0, n-i-1): # Traverse the array from 0 to n-i-1
if arr[j] > arr[j+1]: # Swap if the element found
arr[j], arr[j+1] = arr[j+1], arr[j] # greater than next element
def main():
arr = [12, 96, 24, 48, 36, 60, 84, 72] # Declaring a sample array
bubble_sort(arr) # Calling the bubbleSort function and passing the array
print('Sorted array is: {}'.format(' '.join(map(str, arr))))
if __name__ == '__main__':
main()