Python选择排序问题

时间:2016-05-08 15:04:07

标签: python sorting

所以我在这里有一个代码块用于python中的选择排序。我理解选择排序的概念。在列表中搜索最小值,将其与列表中的第一个值交换,并对列表中的其余元素重复。我已经尝试过写出来了,而不是排序列表############################################################# ################### OFFICIAL UBUNTU REPOS ################### ############################################################# ###### Ubuntu Main Repos deb http://us.archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse deb-src http://us.archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse ###### Ubuntu Update Repos deb http://us.archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse deb http://us.archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse deb http://us.archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse deb-src http://us.archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse deb-src http://us.archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse deb-src http://us.archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse ###### Ubuntu Partner Repo deb http://archive.canonical.com/ubuntu precise partner deb-src http://archive.canonical.com/ubuntu precise partner ###### Ubuntu Extras Repo deb http://extras.ubuntu.com/ubuntu precise main deb-src http://extras.ubuntu.com/ubuntu precise main 我得到了[3,7,4,9,9]。我试过跟踪,但我找不到错误。我非常感谢你的帮助。

[2,3,4,7,9]

1 个答案:

答案 0 :(得分:2)

    if minimum  > T[j]:
        minIndex = j

只更新索引,minimum值也需要更新:

    if minimum  > T[j]:
        minIndex = j
        minimum = T[j]    # <---