嗨,我正在尝试冒泡。
这是特殊指定任务的一部分,但我仍然坚持使用该程序的python代码。我确实试过做一个程序,但它运行一个无限循环而不是冒泡排序。
答案 0 :(得分:1)
以下是您在Python代码中的答案!
#This is the list that will be sorted.
list1 = [87,4,23,100,29,7,16,56,576,45,23,34,546,7,8,9,4,3,567,1,484,3274,73]
#This keeps the while loop going.
change = 1
#This holds the list reference of the value.
a = 0
#This holds the actual value in the list that is compared.
b = 0
#This holds the next value in the list.
c = 0
#This keeps the program going while it is still sorting.
while change == 1:
#This defines b.
b = list1[a]
#This defines c.
c = list1[a + 1]
#This checks a is bigger than c.
if b > c:
#This reassigns the values in the right places.
list1[a+1] = b
list1[a] = c
#Registers a change.
g = 0
#This checks if a can be increased and still be in the list.
if a + 2 < len(list1):
#This makes a larger to test different elements.
a = a + 1
#If none prior are done then the program does this.
else:
#This resets a to go through the list again.
a = 0
#This records that there has been no change.
g = g + 1
#This makes it stop.
if g == len(list1):
change = 0
print(list1)