我正在尝试创建一个程序,该程序将允许用户输入一个数字列表,该列表将根据用户的选择通过冒泡,快速,插入或合并排序进行排序。
我正在测试冒泡排序的选择,但是有问题。当从要打印的子例程中取出内容时,变量号将不会打印,其内容是相同的输入列表。
谁能建议一种解决方法,以使气泡排序在用作子例程时正常运行?仅供参考:python的版本是3.6.2,并且不在子例程中时,冒泡排序将起作用。
it 'returns the number provided' do
# instantiate object that we will test
subject = RubyOperations::Operations.new
# we stub method 'gets' and whenever it is called we return string "5"
allow(subject).to receive(:gets) { "5" }
# we call method input with argument 1, the argument 1 is pointless as described in point 1) and can be omitted
expect(subject.input(1)).to eq(5)
end
答案 0 :(得分:0)
您应该使bubble
函数return
成为结果的numbers
列表,以便您的主程序可以打印它。请注意,对于冒泡排序实现是错误的,因为它的上限应涵盖所有项目:
def bubble(numbers):
import time
start = time.time()
numItems = len(numbers)
for i in range (0, numItems):
for j in range (0, numItems - i - 1):
if numbers [j] > numbers [j + 1]:
temp = numbers[j]
numbers[j] = numbers[j + 1]
numbers[j + 1] = temp
end = time.time()
print (end - start)
return numbers
print ("Bubble, merge, quick and insertion sort visulizer.")
numbers = [int(x) for x in input("Input your list ").split()]
numbers = bubble(numbers)
print(numbers)