Ответ:
# цикл for
mass = [5, 3, 2, 4, 8, 1, 0, 9, 7]
print(mass)
n = len(mass)
for i in range(1, n):
for j in range(n - i):
if mass[j] > mass[j + 1]:
mass[j], mass[j + 1] = mass[j + 1], mass[j]
# цикл while
i = 1
while i < n:
j = 0
while j < n - i:
j += 1
i += 1
# процедура
def bubble_sort(a):
n = len(a)
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
return a
print(bubble_sort(mass))
Объяснение:
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Ответ:
# цикл for
mass = [5, 3, 2, 4, 8, 1, 0, 9, 7]
print(mass)
n = len(mass)
for i in range(1, n):
for j in range(n - i):
if mass[j] > mass[j + 1]:
mass[j], mass[j + 1] = mass[j + 1], mass[j]
print(mass)
# цикл while
mass = [5, 3, 2, 4, 8, 1, 0, 9, 7]
print(mass)
n = len(mass)
i = 1
while i < n:
j = 0
while j < n - i:
if mass[j] > mass[j + 1]:
mass[j], mass[j + 1] = mass[j + 1], mass[j]
j += 1
i += 1
print(mass)
# процедура
def bubble_sort(a):
n = len(a)
for i in range(1, n):
for j in range(n - i):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
return a
mass = [5, 3, 2, 4, 8, 1, 0, 9, 7]
print(mass)
print(bubble_sort(mass))
Объяснение: