#сортировки по возрастанию
def bubble_sort_for(lst):
for i in range(len(lst)):
for j in range(len(lst)-1):
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
return lst
def bubble_sort_while(lst):
i = 0
while i < len(lst):
j = 0
while j < len(lst)-1:
j += 1
i += 1
print(bubble_sort_for([5, 4, 3, 2, 1, -1]))
print(bubble_sort_while([5, 4, 3, 2, 1, -1]))
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#сортировки по возрастанию
def bubble_sort_for(lst):
for i in range(len(lst)):
for j in range(len(lst)-1):
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
return lst
def bubble_sort_while(lst):
i = 0
while i < len(lst):
j = 0
while j < len(lst)-1:
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
j += 1
i += 1
return lst
print(bubble_sort_for([5, 4, 3, 2, 1, -1]))
print(bubble_sort_while([5, 4, 3, 2, 1, -1]))