-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBubbleSort.py
More file actions
36 lines (25 loc) · 721 Bytes
/
BubbleSort.py
File metadata and controls
36 lines (25 loc) · 721 Bytes
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
#implementing bubble sort on a list
#create a randomly sorted list
import random as r
L1 = []
for i in range(1,200,2):
L1.append(i)
r.shuffle(L1) #note that this MUTATES the list
#sorting the list in ascending order
def sort_function(L):
print(L)
n = len(L)
while True:
s= 0 #counter for recording the number of swaps in the current pass
for i in range(n-1):
temp = 0
if L[i+1] < L[i]:
temp = L[i]
L[i] = L[i+1]
L[i+1] = temp
s+=1
if s == 0:
print (L)
return L
#testing
sort_function(L1)