泡沫排序Python

By simon at 2018-02-07 • 0人收藏 • 44人看过

我正在尝试使用Python进行排序,而不是进行函数的导入 功能等 我已经得到了这个,但现在我难倒:页

array = [1, 42, 321, 44, 121, 61, 812, 71, 10, 11]
number = 1
ArrayIndex = 0
numchange = 0
TotalNumberofLoops = 10
OuterLoop = 0
InnerLoop = 0

while OuterLoop < TotalNumberofLoops:
  InnerLoop = OuterLoop + 1
  while InnerLoop < TotalNumberofLoops:
    if array[OuterLoop] < array[InnerLoop]:
      numchange = array[InnerLoop]
      array[OuterLoop] = array[InnerLoop]
      array[InnerLoop] = numchange

    InnerLoop=InnerLoop + 1
  print array
  OuterLoop = OuterLoop + 1
这给出了以下输出:
[812, 42, 321, 44, 121, 61, 812, 71, 10, 11]
[812, 812, 321, 44, 121, 61, 812, 71, 10, 11]
[812, 812, 812, 44, 121, 61, 812, 71, 10, 11]
[812, 812, 812, 812, 121, 61, 812, 71, 10, 11]
[812, 812, 812, 812, 812, 61, 812, 71, 10, 11]
[812, 812, 812, 812, 812, 812, 812, 71, 10, 11]
[812, 812, 812, 812, 812, 812, 812, 71, 10, 11]
[812, 812, 812, 812, 812, 812, 812, 71, 10, 11]
[812, 812, 812, 812, 812, 812, 812, 71, 11, 11]
[812, 812, 812, 812, 812, 812, 812, 71, 11, 11]
感谢任何解决方案!

2 个回复 | 最后更新于 2018-02-07
2018-02-07   #1

尝试这个:

array = [1, 42, 321, 44, 121, 61, 812, 71, 10, 11]
number = 1
ArrayIndex = 0
numchange = 0
TotalNumberofLoops = 10
OuterLoop = 0
InnerLoop = 0

while OuterLoop < TotalNumberofLoops:
    InnerLoop = OuterLoop + 1
    while InnerLoop < TotalNumberofLoops:
        if array[OuterLoop] < array[InnerLoop]:
            array[OuterLoop], array[InnerLoop] = array[InnerLoop], array[OuterLoop]

        InnerLoop = InnerLoop + 1
    print(array)
    OuterLoop = OuterLoop + 1
这样,元素交换更加pythonic也是正确的。

2018-02-07   #2

尝试这个:

array = [1, 42, 321, 44, 121, 61, 812, 71, 10, 11]
number = 1
ArrayIndex = 0
numchange = 0
TotalNumberofLoops = 10
OuterLoop = 0
InnerLoop = 0

while OuterLoop < TotalNumberofLoops:
    InnerLoop = OuterLoop + 1
    while InnerLoop < TotalNumberofLoops:
        if array[OuterLoop] < array[InnerLoop]:
            array[OuterLoop], array[InnerLoop] = array[InnerLoop], array[OuterLoop]

        InnerLoop = InnerLoop + 1
    print(array)
    OuterLoop = OuterLoop + 1
这样,元素交换更加pythonic也是正确的。

登录后方可回帖

Loading...