-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.py
More file actions
22 lines (18 loc) · 747 Bytes
/
bubblesort.py
File metadata and controls
22 lines (18 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def bubblesort (theList):
panjangList = len(theList)
#Transverse seluruh elemen array
for i in range(panjangList):
swapped = False
#Elementerakhir yang telah ada di tempatnya
for j in range (0,panjangList-i-1):
#Arraynya di transverse dari 0 ke panjangList-i-1.
#Swap element jika elemen tersebut lebih besar dari elemen berikutnuya
if(theList[j]>theList[j+1]):
theList[j], theList[j+1] = theList[j+1],theList[j]
swapped = True
#Kalau kedua elemet swap by inner loop, then break
if swapped == False:
break
listTest = [19, 41, 24, 50, 41, 50]
bubblesort(listTest)
print(listTest) #[12, 19,24, 41, 50]