Answer the question
In order to leave comments, you need to log in
How to make a cycle counter?
What is the correct way to insert a loop counter in this code?
a = [8,1,7,4,3,9,2,5,6,10]
def bubble_sort(a):
n = len(a)
unordered = True
while unordered:
unordered = False
for j in range(n - 1):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
unordered = True
n -= 1
Answer the question
In order to leave comments, you need to log in
It is possible like this:
a = [8,1,7,4,3,9,2,5,6,10]
def bubble_sort(a):
n = len(a)
unordered = True
operations_count = 0
while unordered:
unordered = False
operations_count += 1
for j in range(n - 1):
operations_count += 1
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
unordered = True
n -= 1
print(str(operations_count))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question