21 lines
389 B
Python
21 lines
389 B
Python
|
def bubble_sort(arr):
|
||
|
n = len(arr)
|
||
|
|
||
|
count = 0
|
||
|
for i in range(n):
|
||
|
flag = True
|
||
|
for j in range(0, n - i - 1):
|
||
|
if arr[j] > arr[j + 1]:
|
||
|
arr[j], arr[j + 1] = arr[j + 1], arr[j]
|
||
|
flag = False
|
||
|
count += 1
|
||
|
|
||
|
if flag:
|
||
|
break
|
||
|
|
||
|
return count
|
||
|
|
||
|
|
||
|
s = list('jonmlkihgfedcba')
|
||
|
print(bubble_sort(s))
|