冒泡排序作业 [英] Bubble Sort Homework
问题描述
在课堂上,我们正在做排序算法,虽然我知道他们很好,当谈到他们和书面的伪code,我有写实际code为他们的问题。
In class we are doing sorting algorithms and, although I understand them fine when talking about them and writing pseudocode, I am having problems writing actual code for them.
这是我在Python的尝试:
This is my attempt in Python:
mylist = [12, 5, 13, 8, 9, 65]
def bubble(badList):
length = len(badList) - 1
unsorted = True
while unsorted:
for element in range(0,length):
unsorted = False
if badList[element] > badList[element + 1]:
hold = badList[element + 1]
badList[element + 1] = badList[element]
badList[element] = hold
print badList
else:
unsorted = True
print bubble(mylist)
现在,这个(据我可以告诉)排序正确的,但一旦它完成它只是循环下去。
Now, this (as far as I can tell) sorts correctly, but once it finishes it just loops indefinitely.
这怎么code是固定的,因此函数完成正常和正确排序任何(合理的)尺寸的列表?
How can this code be fixed so the function finishes properly and correctly sorts a list of any (reasonable) size?
P.S。我知道,我真的不应该有打印的功能,我应该有回报,但我没有这样做还因为我的code并没有真正的工作还没有。
P.S. I know I should not really have prints in a function and I should have a return, but I just have not done that yet as my code does not really work yet.
推荐答案
要解释为什么你的脚本不能正常工作,现在,我要重命名变量未分类
来排序
。
To explain why your script isn't working right now, I'll rename the variable unsorted
to sorted
.
首先,你的名单尚未排序。当然,我们设置排序
到假
。
At first, your list isn't yet sorted. Of course, we set sorted
to False
.
一旦我们开始了,而
循环中,我们假设该列表已经排序。我们的想法是这样的:只要我们发现两个元素是不是在正确的顺序,我们设置排序
返回假
。 排序
仍将真
的只是,如果有错误的顺序没有元素的。
As soon as we start the while
loop, we assume that the list is already sorted. The idea is this: as soon as we find two elements that are not in the right order, we set sorted
back to False
. sorted
will remain True
only if there were no elements in the wrong order.
sorted = False # We haven't started sorting yet
while not sorted:
sorted = True # Assume the list is now sorted
for element in range(0, length):
if badList[element] > badList[element + 1]:
sorted = False # We found two elements in the wrong order
hold = badList[element + 1]
badList[element + 1] = badList[element]
badList[element] = hold
# We went through the whole list. At this point, if there were no elements
# in the wrong order, sorted is still True. Otherwise, it's false, and the
# while loop executes again.
也有轻微的小问题,这将有助于在code更有效和可读性。
There are also minor little issues that would help the code be more efficient or readable.
-
在
为
循环,可以使用变量元素
。从技术上来说,元素
不是一个元素;这是一些重presenting一个列表索引。此外,这是相当长的。在这种情况下,只需使用一个临时变量的名称,如我
为指数。
In the
for
loop, you use the variableelement
. Technically,element
is not an element; it's a number representing a list index. Also, it's quite long. In these cases, just use a temporary variable name, likei
for "index".
for i in range(0, length):
在范围
命令也可以只有一个参数(名为停止
)。在这种情况下,你的所有整数列表从0到这样的说法。
The range
command can also take just one argument (named stop
). In that case, you get a list of all the integers from 0 to that argument.
for i in range(length):
建议变量小写用下划线被命名为 Python的风格指南。这是一个非常小的鸡蛋里挑骨头一点点这样的脚本;它更让你习惯什么的Python code最常类似。
The Python Style Guide recommends that variables be named in lowercase with underscores. This is a very minor nitpick for a little script like this; it's more to get you accustomed to what Python code most often resembles.
def bubble(bad_list):
要交换两个变量的值,给他们写一个元组分配。右手边被评价为一个元组(例如,(badList [I + 1],badList [I])
是(3,5)
),然后被分配到左侧的两个变量((badList [I],badList [I + 1])
)。
To swap the values of two variables, write them as a tuple assignment. The right hand side gets evaluated as a tuple (say, (badList[i+1], badList[i])
is (3, 5)
) and then gets assigned to the two variables on the left hand side ((badList[i], badList[i+1])
).
bad_list[i], bad_list[i+1] = bad_list[i+1], bad_list[i]
把它放在一起,你会得到这样的:
Put it all together, and you get this:
my_list = [12, 5, 13, 8, 9, 65]
def bubble(bad_list):
length = len(bad_list) - 1
sorted = False
while not sorted:
sorted = True
for i in range(length):
if bad_list[i] > bad_list[i+1]:
sorted = False
bad_list[i], bad_list[i+1] = bad_list[i+1], bad_list[i]
bubble(my_list)
print my_list
(我删除了您的打印语句也顺便说一句。)
(I removed your print statement too, by the way.)
这篇关于冒泡排序作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!