Python列表 - 代码和&算法

时间:2013-11-22 22:13:15

标签: python list recursion

我需要一些帮助python,一种新的程序语言给我。 所以,让我说我有这个清单:

list= [3, 1, 4, 9, 8, 2]

我想对它进行排序,但不使用内置功能“排序”,否则这里的所有乐趣和学习都在哪里?我想编写尽可能简单和基本的代码,即使它意味着更难以工作。因此,如果您想帮助我并向我提供一些想法和代码,请尽量使它们非常“基本”。

无论如何,回到我的问题:为了对这个列表进行排序,我决定每次从列表中的数字到最后一个数字进行比较。首先,我将检查3和2.如果3小于2(并且它是假的,错误的),那么什么都不做。

接下来 - 检查1是否小于2(并且为真) - 然后使用第一个元素更改此数字的索引位置。 在下一次运行时,它将再次检查该数字是否小于列表中的最后一个数字。但是这一次,如果数字较小,它将用第二个数字改变位置(并且在第三次运行时用第三个数字改变,如果它更小,当然)。

依旧等等。 最后,()函数将返回排序列表。 跳你明白了。 所以我想使用()递归函数使任务有点兴趣,但仍然是基本的。 因此,我想到了这段代码:

def func(list):
if not list:
    for i in range(len(list)):
        if list[-1] > lst[i]:
           #have no idea what to write here in order to change the locations
            i = i + 1
        #return func(lst[i+1:])?
    return list

2个问题: 1.我如何更改位置?使用pop / remove然后插入? 2.我不知道在哪里放置递归部分,如果我写得好(我想我没有)。递归部分是第二个“#”,第一个“返回”。

你怎么看?我该如何改进这段代码?怎么了?

非常感谢!

3 个答案:

答案 0 :(得分:1)

哦,伙计,整理。这是编程中最受欢迎的问题之一,许多解决方案在每种语言中都有所不同。无论如何,最直接的算法是我猜bubble sort。但是,它不是很有效,所以它主要用于教育目的。如果你想尝试一些更高效和更常见的东西去quick sort。我相信这是最流行的排序算法。但是在python中,默认算法有点不同 - read here。就像我说的那样,网络上有很多很多的排序算法。

现在,回答你的具体问题:在python中替换列表中的项目就像

一样简单
list[-1]=list[i]

tmp=list[-1]
list[-1]=list[i]
list[i]=tmp

关于递归 - 我认为使用它不是一个好主意,这里简单的while / for循环更好。

答案 1 :(得分:0)

也许你可以用这种方式试试快餐:

def quicksort(array, up, down):
    # start sorting in your array from down to up :
    # is array[up] < array[down] ? if yes switch
    # do it until up <= down 
    # call recursively quicksort 
    #        with the array, middle, up
    #        with the array, down, middle
    # where middle is the value found when the first sort ended

您可以查看以下链接:Quicksort on Wikipedia 这几乎是一样的逻辑。

希望它会有所帮助!

答案 2 :(得分:0)

  1. 交换两个列表元素的最简单方法是使用“并行分配”:

    list[-1], list[i] = list[i], list[-1]
    
  2. 对此算法使用递归并没有多大意义。如果您调用func(lst[i+1:]),则会对列表中的这些元素进行复制,并且对该副本执行递归调用,然后丢弃该副本。你可以让func取两个参数:列表和i+1

  3. 但你的代码仍然破碎。 not list测试不正确,i = i + 1不正确。您所描述的内容听起来是selection sort的变体,您正在进行一系列额外的交换。

    以下是选择排序的正常运作方式。

    1. 找出所有元素中最小的元素并将其交换为索引0。
    2. 查找所有剩余元素中的最小元素(所有索引大于0)并将其交换为索引1。
    3. 找出所有剩余元素中最小的元素(所有索引大于1)并将其交换为索引2.
    4. 等等。
    5. 为了简化,算法是这样的:找到所有剩余(未排序)元素中最小的元素,并将其附加到已排序元素列表中。重复,直到没有剩余的未排序元素。

      我们可以用Python编写它:

      def func(elements):
          for firstUnsortedIndex in range(len(elements)):
              # elements[0:firstUnsortedIndex] are sorted
              # elements[firstUnsortedIndex:] are not sorted
              bestIndex = firstUnsortedIndex
              for candidateIndex in range(bestIndex + 1, len(elements)):
                  if elements[candidateIndex] < elements[bestIndex]:
                      bestIndex = candidateIndex
              # Now bestIndex is the index of the smallest unsorted element
              elements[firstUnsortedIndex], elements[bestIndex] = elements[bestIndex], elements[firstUnsortedIndex]
              # Now elements[0:firstUnsortedIndex+1] are sorted, so it's safe to increment firstUnsortedIndex
          # Now all elements are sorted.
      

      测试:

      >>> testList = [3, 1, 4, 9, 8, 2]
      >>> func(testList)
      >>> testList
      [1, 2, 3, 4, 8, 9]
      

      如果你真的想要构建这个以便递归有意义,那么这里是怎样的。找到列表中最小的元素。然后递归调用func,传递所有剩余的元素。 (因此每个递归调用传递一个较少的元素,最终传递零元素。)然后将该最小元素添加到递归调用返回的列表中。这是代码:

      def func(elements):
          if len(elements) == 0:
              return elements
          bestIndex = 0
          for candidateIndex in range(1, len(elements)):
              if elements[candidateIndex] < elements[bestIndex]:
                  bestIndex = candidateIndex
          return [elements[bestIndex]] + func(elements[0:bestIndex] + elements[bestIndex + 1:])