【发布时间】:2019-04-05 18:38:21
【问题描述】:
我正在创建一个冒泡排序算法,它接受一个列表并返回 2 个值: 第一个返回值:一个字典,其中包含每次冒泡排序完成后的列表状态 第二个返回值:排序列表
log = {}
for n in range(len(numList)):
for i in range(0, len(numList)-n-1):
# Comparing numbers and swapping them
if numList[i] > numList[i+1]:
numList[i], numList[i+1] = numList[i+1], numList[i]
# Creating log of the state of each pass
log[n+1] = numList
# Returning Results
return log, numList
示例输入:>>> bubbleSort([9,3,5,4,1,67,78])
样本输出:({1: [3, 5, 4, 1, 9, 67, 78], 2: [3, 4, 1, 5, 9, 67, 78], 3: [3, 1, 4, 5, 9, 67, 78], 4: [1, 3, 4, 5, 9, 67, 78], 5: [1, 3, 4, 5, 9, 67, 78]}, [1, 3, 4, 5, 9, 67, 78])
【问题讨论】:
-
你能包括一些示例输入和输出吗?
标签: python arrays dictionary