【问题标题】:Trying to create a sorting algorithm from scratch in Python尝试在 Python 中从头开始创建排序算法
【发布时间】:2016-04-21 20:44:09
【问题描述】:

我正在学习编程课程(我是一个完全的初学者),当前的任务是创建一个 Python 脚本,该脚本可以在不使用“sorted”等内置函数的情况下按升序对数字列表进行排序.

我开始想出的脚本可能是可笑的复杂和低效,但我想尝试让它最终工作。换句话说,我不想只是复制别人更好的脚本。

此外,据我所知,这个脚本(如果它曾经运行过的话)可能只是将事物置于一个不一定是升序的新顺序中。不过,这只是一个开始,希望我稍后会解决这个问题。

无论如何,我在当前的版本中遇到了几个问题,最新的是它永远运行而没有打印任何东西。

就是这样(用散列解释我想要完成的事情)。如果有人可以查看它并告诉我为什么代码与我对每个块应该做什么的解释不符,那就太好了!

# The numbers to be inputted, could be anything
numList = [1, 25, 5, 6, 17, 4]                     

# The final (hopefully sorted) list
numSort = []

# The index
i = 0

# Run the loop until you run out of numbers
while len(numList) != 0:                                       

    # If there's only one value left in numList,
    # attach it to the end of numSort.
    # (Variable 'basket' is just for transporting numbers)
    if len(numList) == 1:                                       
        basket = numList.pop()
        numSort.append(basket)

    # The rest of the elifs are supposed to compare values
    # that sit next to each other.

    # If there's still a number in numList after 'i'
    # and 'i' is smaller than that next number
    # then pop 'i' and attach it to the end of numSort
    elif numList[i+1] != numList[-1] and numList[i] < numList[i+1]:   
        basket = numList.pop(i)                                 
        numSort.append(basket)

    # If there's NOT a number after 'i'
    # then compare 'i' to the first number in the list instead.
    elif numList[i+1] == numList[-1] and numList[i] < numList[0]:      
        basket = numList.pop(i)                                 
        numSort.append(basket)

    # If 'i' IS the last number in the list
    # and has nothing to compare itself to,
    # Then start over and go through it again
    # from the beginning
    elif numList [i+1] == numList[-1]:
        i = 0

    # If 'i' is not at the end of numList yet
    # and 'i' is NOT smaller than the next number
    # and there are still numbers left
    # then move on to the next pair
    # and continue comparing and moving numbers
    else:                                                       
        i = i+1

# Hopefully these will be in ascending order eventually.
print(numSort)

【问题讨论】:

  • 已经,您可以将while len(numList) != 0: 替换为while numList:
  • 我看到的第一个语义错误是numList [i+1] == numList[-1]。这不检查位置i 的数字是否是列表的最后一个数字,它检查位置i+1 的数字是否等于 到列表的最后一个数字。
  • 可能首先使用数组,然后尝试了解 numList[-1] 的作用。
  • 实现其他算法有什么问题吗?比如冒泡排序、选择排序等等……这些都很简单,实现起来也很简单,而且需要的代码也少得多。
  • 当您使用pop 时,索引再次从零开始,因此您将在此行elif numList[i+1] != numList[-1] and numList[i] &lt; numList[i+1] 上获得index error

标签: python list sorting python-3.x


【解决方案1】:

这是一种使用经典循环对列表进行排序的简单方法:

myList = [2,99,0,56,8,1]
for i,value in enumerate(myList):
     for j,innerValue in enumerate(myList):
         if value < innerValue: #for order desc use '>'
             myList[j],myList[i]=myList[i],myList[j]
 print(myList)

这段代码背后的算法是:

  • 修复列表中的一个值并将其与其余值进行比较
  • 如果最小则切换两个值的索引

希望对你有帮助

【讨论】:

    【解决方案2】:

    你的条件本质上是:

    1. 如果列表中只有一个数字
    2. 当前数小于下一个,不等于最后一个数
    3. 当前数小于第一个数,下一个数等于最后一个数
    4. 下一个数字等于最后一个数字

    如果您手动跟踪代码,您将看到在许多情况下,这些都不会评估为 true,并且您的“else”将被执行,并且 i 将被递增。在这些情况下,某些数字将永远不会从您的原始列表中删除(您的任何 elif 都不会捕获它们),我将递增直到下一个数字等于最后一个数字,我将重置为零,重复。你陷入了一个无限循环。您将需要更新您的 if 语句,以使所有数字最终都会被除最终 elif 之外的块捕获。

    在单独的注释中,您可能会将一个数字与当前列表中的一个数字进行比较,然后再将其附加到“排序”列表中。您可能需要比较要添加到“排序”列表中的数字,并在该列表中找到它的适当位置,而不仅仅是追加。

    您还应该考虑使用类似的方法查找列表末尾

    if i == len(numList) - 1
    

    这会将索引与列表的长度进行比较,而不是比较列表中与您尝试创建的顺序不一定相关的更多值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2020-04-21
      • 2021-04-12
      • 2021-11-18
      • 2016-03-04
      相关资源
      最近更新 更多