【问题标题】:how to fix the sort function如何修复排序功能
【发布时间】:2017-02-11 06:57:42
【问题描述】:
def merge (l1,l2):
    if l1 and l2:
        if l1 == [] and l2 == []:
            return []
        if l1[0] > l2[0]:
            l1, l2 = l2, l1 
        return [l1[0]] + merge(l1[1:], l2)
    return l1 + l2


def sort(l):
    x = len(l) / 2
    x = int(x)
    y = merge(l[0:x], l[x+1:])
    return y

我需要编写一个名为 sort 的递归函数;它被传递任何无序列表(全部为 int 或全部为 str),并返回一个新列表,该列表包含其参数列表中的每个值,但按排序/非降序排列。但是我不能调用任何执行排序的 Python 函数/方法。

另外,对于任何至少有 2 个值的列表,我必须将列表分成两半并递归调用 sort 来对每个较小的列表进行排序,我必须使用上面编写的合并函数来合并这两个排序的列表从这些递归调用返回

merge 是对两个列表进行合并和排序的函数

merge([1,3,5,8,12],[2,3,6,7,10,15]) returns [1,2,3,3,5,6,7,8,10,12,15].

例如,调用 sort([4,5,3,1,6,7,2]) 将在列表 [4,5,3] 和 [1,6,7,2] 上递归调用 sort) ,分别返回列表 [3,4,5] 和 [1,2,6,7],合并后将返回列表 [1,2,3,4,5,6,7]。

我的函数出现以下错误

39 *Error: sort([1,2,3,4,5,6,7]) -> [1, 2, 3, 5, 6, 7] but should -> [1, 2, 3, 4, 5, 6, 7]
40 *Error: sort([7,6,5,4,3,2,1]) -> [3, 2, 1, 7, 6, 5] but should -> [1, 2, 3, 4, 5, 6, 7]
41 *Error: sort([4,5,3,1,2,7,6]) -> [2, 4, 5, 3, 7, 6] but should -> [1, 2, 3, 4, 5, 6, 7]
42 *Error: sort([1,7,2,6,3,5,4]) -> [1, 3, 5, 4, 7, 2] but should -> [1, 2, 3, 4, 5, 6, 7]

我的排序方法有什么问题?有人可以帮我解决吗?提前致谢。

【问题讨论】:

    标签: python recursion


    【解决方案1】:

    三个问题:

    • 你的y = merge(l[0:x], l[x+1:]) 输了l[x],改成y = merge(l[:x], l[x:])
    • 它不会对两半进行排序,所以将其设为y = merge(sort(l[:x]), sort(l[x:]))
    • 您没有基本情况,在无事可做时停止递归。

    更正并简化了一点:

    def sort(l):
        if len(l) <= 1:
            return l
        x = len(l) // 2
        return merge(sort(l[:x]), sort(l[x:]))
    

    // 是整数除法,因此您不需要额外的int(...)。并且没有必要创建 y 变量。


    顺便说一句,if l1 and l2: 中的 if l1 == [] and l2 == []: 测试毫无意义(如果 l1l2[],那么您首先就不会进入 if l1 and l2: 块),所以你可以删除它。


    还有一件事:虽然您的 merge 函数没有错,但它很慢。每个l1[1:] 花费的时间与l1 的长度成正比。您最好使用索引,例如在 Huy Vo 的回答中。

    【讨论】:

      【解决方案2】:

      好吧,基本上你所做的一切都是多余的。

      list1 = [1,3,5,8,12]
      list2 = [2,3,6,7,10,15]
      
      list3 = list1 + list2 # Merges lists
      
      list3_sorted = sorted(list3) # Sorts them
      

      还有一点好处,如果您有一个列表或元组列表,并且您想按每个列表或元组的索引进行排序

      from operator import itemgetter
      
      list = [(2,6), (3,4)]
      list_sorted = sorted( list, key=itemgetter(1) ) # Will sort by index 1 of each item.
      

      编辑:我现在意识到你不能使用任何内置函数,请给我一些东西,看看我是否能解决问题

      【讨论】:

      • “但是我不能调用任何 Python 的函数/方法来执行排序”
      • 你没有仔细阅读我的描述,我不能调用任何 Python 的函数/方法来执行排序。另外,我需要在 sort 函数中调用 merge 方法
      • 好的,抱歉我没有读到。我会调查并更新我的答案。
      【解决方案3】:

      你需要的是归并排序,我相信网上有多种归并排序伪代码。

      无论如何,这是我的 Python 3 版本:

      def mergesort(lst):
          if len(lst) < 2:
              return lst
          else:
              middle = len(lst) // 2
              # recursion, baby
              left_half = mergesort(lst[:middle])
              right_half = mergesort(lst[middle:])
              return merge(left_half, right_half)
      
      def merge(left, right):
          result = []
          i, j = 0, 0
          while i < len(left) and j < len(right):
              if left[i] <= right[j]:
                  result.append(left[i])
                  i += 1
              elif left[i] > right[j]:
                  result.append(right[j])
                  j += 1
          result += left[i:] + right[j:]
          return result
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多