为什么这个python代码要花这么长时间?

为什么这个python代码要花这么长时间?,python,sorting,Python,Sorting,好的,我有一段比较合并排序和选择排序的python代码,但这需要花费很长时间。当从n=0到90000(列表的大小)进行排序时,只需大约3秒钟即可对列表进行排序。按照这种逻辑,大约需要10*3*9秒(通过次数*持续时间*增加的通过次数[我们从10000开始,然后进行20000,然后是30000,等等])。然而,这需要的时间远不止这些 import time import random # Selection Sort Code # def maxIndex(J): return J.in

好的,我有一段比较合并排序和选择排序的python代码,但这需要花费很长时间。当从n=0到90000(列表的大小)进行排序时,只需大约3秒钟即可对列表进行排序。按照这种逻辑,大约需要10*3*9秒(通过次数*持续时间*增加的通过次数[我们从10000开始,然后进行20000,然后是30000,等等])。然而,这需要的时间远不止这些

import time
import random

# Selection Sort Code #
def maxIndex(J):
    return J.index(max(J))

def swap(LCopy, i, j):
    temp = LCopy[i]
    LCopy[i] = LCopy[j]
    LCopy[j] = temp

# Implementation of selection sort
def selectionSort(L):
    for i in range(len(L)-1, 1, -1):
        j = maxIndex(L[0:i+1])
        swap(L, i, j)

# Merge Sort Code #
# Assumes that L[first:mid+1] is sorted and also
# that L[mid: last+1] is sorted. Returns L with L[first: last+1] sorted

def merge(L, first, mid, last):

    i = first # index into the first half
    j = mid + 1 # index into the second half

    tempList = []

    # This loops goes on as long as BOTH i and j stay within their
    # respective sorted blocks
    while (i <= mid) and (j <= last):
        if L[i] <= L[j]:
            tempList.append(L[i])
            #print L[i], "from the first block"
            i += 1
        else:
            tempList.append(L[j])
            #print L[j], "from the second block"
            j += 1

    # If i goes beyond the first block, there may be some elements
    # in the second block that need to be copied into tempList.
    # Similarly, if j goes beyond the second block, there may be some
    # elements in the first block that need to be copied into tempList
    if i == mid + 1:
        tempList.extend(L[j:last+1])
        #print L[j:last+1], "some elements in second block are left over"
    elif j == last+1:
        tempList.extend(L[i:mid+1])
        #print L[i:mid+1], "some elements from first block are left over"

    L[first:last+1] = tempList
    #print tempList


# The merge sort function; sorts the sublist L[first:last+1]    
def generalMergeSort(L, first, last):
    # Base case: if first == last then it is already sorted

    # Recursive case: L[first:last+1] has size 2 or more
    if first < last:
        # divide step
        mid = (first + last)/2

        # conquer step
        generalMergeSort(L, first, mid)
        generalMergeSort(L, mid+1, last)

        # combine step
        merge(L, first, mid, last)

# Wrapper function
def mergeSort(L):
    generalMergeSort(L, 0, len(L)-1)



m = 10
n = 100000
n_increments = 9
baseList = [ random.randint(0,100) for r in range(n) ]


i = 0

while i < n_increments:
    j = 0
    sel_time = 0
    mer_time = 0

    while j < m:
        # Do a Selection Sort #
        x = time.clock()

        selectionSort( baseList)

        y = time.clock()

        sel_time += ( y - x )

        random.shuffle( baseList )

        # Do a Merge Sort #

        x = time.clock()

        mergeSort( baseList )

        y = time.clock()

        mer_time += ( y - x )

        random.shuffle( baseList )

        j += 1
    print "average select sort time for a list of", n, "size:", sel_time / m
    print "average merge sort time for a list of", n, "size:", mer_time / m

    j = 0
    i += 1
    n += 10000
导入时间
随机输入
#选择排序代码#
def最大索引(J):
返回J.索引(最大值(J))
def交换(LCopy,i,j):
温度=LCopy[i]
LCopy[i]=LCopy[j]
LCopy[j]=温度
#选择排序的实现
def选择排序(L):
对于范围内的i(len(L)-1,1,-1):
j=maxIndex(L[0:i+1])
互换(L、i、j)
#合并排序代码#
#假设L[first:mid+1]已排序,并且
#L[mid:last+1]已排序。返回L,并对L[first:last+1]进行排序
def合并(L、第一、中间、最后):
i=第一个#索引到上半部分
j=中期+1#指数进入下半年
圣殿骑士=[]
#只要i和j都在各自的范围内,循环就会继续
#各自的排序块

而(i是因为您使用的是O(n^2)排序算法。这意味着,如果将n加倍,则算法运行时间将延长4倍。请注意,您从100000开始,而不是10000开始。这是否是一个合适的问题?当您增加n时,您的代码实际上并没有增加列表的大小。Move
baseList=[random.randint(0100)对于范围(n)内的r)]
进入外部while循环以修复此问题。@Acorn我不知道stackexchange站点存在…@Eric谢谢!我做了更改并再次开始运行程序。尽管如此,看起来仍然很缓慢。