繁体   English   中英

Python冒泡排序

[英]Python Bubble sort

嘿所以代码工作非常非常感谢你!

我还有一个问题。 我想在两个coulmns之间显示一个箭头。 我创建了这段代码,但我真的不知道如何让它进入正在切换的列之间。 有什么建议么?

def arrow(lst, i):                 # function for the arrow
if (lst[i], lst[i+1] == lst[i+1], lst[i]):
    t.home()
    t.penup()
                     # im thinking something goes here but i dont know what :P
    t.pendown()
    t.pencolor("red")
    t.right(90)
    t.forward(20)

任何帮助将非常感谢! 谢谢! 顺便说一句其余的代码就像imran的代码! :) 谢谢!

我根据你的代码编写了一个冒泡排序:

import types

def bubble_sort(lst):
    assert(type(lst)==types.ListType)
    for index in range(1,len(lst)):
        while index > 0 and lst[index-1] > lst[index]:
            lst[index-1] , lst[index] = lst[index] , lst[index-1]
            index -= 1
    return

lst = input("Enter list to be sorted: ")
print "Original: ",lst
bubble_sort(lst)
print "Sorted: ",lst

测试看起来像:

C:\Users\NAME\Desktop>bubble.py
Enter list to be sorted: [4, 24, 25, 2, 6, -1, 73, 1]
Original:  [4, 24, 25, 2, 6, -1, 73, 1]
Sorted:  [-1, 1, 2, 4, 6, 24, 25, 73]

希望能帮助到你!

从原始代码中可以编辑一些内容,最好有两个for循环来检查这个值。 在您使用[i-1] ,如果有意义的话,你会从左到右排序,从而打破冒泡的目的。 无论如何,这是我所做的。

def swap(lst):
    for i in range (len(lst)):
        for j in range (len(lst)-1):
            if lst[j] > lst[j+1]:
                lst[j], lst[j+1] = lst[j+1], lst[j]
                print (lst)


lst = input("Enter list to be sorted: ")
print (lst)
swap(lst)

你的代码看起来很像这里的冒泡排序版本。 您的版本无法正常工作的主要问题是因为您正在将索引i处的lst与i = 1进行比较,而i-1在i = 0时会失败。 将该部分更改为:

        if (lst[i] < lst[i + 1]):
            swapped = False
            lst[i+1], lst[i] = lst[i], lst[i+1]

此外,不要在函数外部定义n(理想情况下应该称为bubble_sort或类似的东西),这是错误的编程,等待发生的错误。 我不知道你要用lst = [lst]来完成什么。 可能想要使用不同的变量名称。

编辑:我对你的代码做了一些自由的修改。 我将所有绘图调用放在冒泡排序中。 冒泡排序还在内部将列表转换为元组列表,其中元组的第二个元素是颜色。 代码末尾的for循环不会做任何有价值的事情。 我认为你的主要问题是它在while循环的每次迭代之间没有暂停,因此你不会看到列表的元素冒泡。

#!/usr/bin/python
import turtle as t

def draw(lst, width):
  for x in lst:
    height = x[0]
    t.color(x[1])
    t.forward(width)
    t.left(90)
    t.forward(height)
    t.left(90)
    t.forward(width)
    t.left(90)
    t.forward(height)
    t.left(90)
    t.penup()
    t.forward(30)
    t.pendown()

def bubble_sort(orig_lst):  
  lst = [ (x, "blue") for x in orig_lst ]
  n = len(lst)-1 
  t.pensize(3)
  draw(lst, width)
  swapped = True  
  while swapped:  
    swapped = False  
    for i in range(n):  
        if lst[i+1][0] < lst[i][0]:  
            lst[i], lst[i+1] = lst[i+1], lst[i]
            lst[i] = (lst[i][0], "green")  
            swapped = True 
            next = raw_input("hit any key to continue ")
            t.home()
            t.clear() 
            draw(lst,width)
  newLst = [ x[0] for x in lst ]
  return newLst

# TOP LEVEL
original_lst = input("Enter list to be sorted: ")
width = input("Provide the width: ")

newLst = bubble_sort(original_lst)
print "Done! Sorted list is: ", newLst
next = raw_input("hit any key to exit ")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM