繁体   English   中英

使用最后一个元素作为枢轴在python中快速排序

[英]Quick Sort in python with last element as pivot

我已经成功测试了我的代码,它可以与最后一个元素一起使用。 但是,当我尝试计算总数时。 进行比较时,显示计数错误。 我正在通过全局变量tot_comparisons进行计数。

建议,我要去哪里错了? 我正在犯一些愚蠢的错误吗?

def swap(A,i,k):
    temp=A[i]
    print "temp is "
    print temp
    A[i]=A[k]
    A[k]=temp

def partition(A,start,end): 
    pivot=A[end]   
    pivot_index=start
    #pivot=A[end]


    for i in range(start,end):   

        #if A[i]<=pivot:
        if A[i]<pivot:
            print 'HHHHHHHHHHHHHhh'
            swap(A,i,pivot_index)
            pivot_index+=1
    #swap(A,pivot_index,end)
    swap(A,pivot_index,end)
    return pivot_index

def quicksort(A,start,end):
    global tot_comparisons
    if start<end:

        pivot_index=partition(A,start,end)
        tot_comparisons+=end-start
        print "pivot_index"
        print pivot_index
        print "ENDS"


        quicksort(A, start,pivot_index-1)

        #tot_comparisons+=end-pivot_index
        #quicksort(A, pivot_index, end)

        quicksort(A, pivot_index+1, end)

#A=[45,21,23,4,65]

#A=[21,23,19,22,1,3,7,88,110]
#A=[1,22,3,4,66,7]

#A=[1, 3, 7, 19, 21, 22, 23, 88, 110]
#A=[7,2,1,6,8,5,3,4]

temp_list=[]
f=open('temp_list.txt','r')
for line in f:
    temp_list.append(int(line.strip()))
f.close()
print 'list is '
#print temp_list
print 'list ends'

tot_comparisons=0
#quicksort(A, 0, 7)
quicksort(temp_list, 0, 9999)
#quicksort(temp_list, 0, len(temp_list))
print 'hhh'
print temp_list
print tot_comparisons
#print A

我检查了您的quicksort是否有效,尽管它与流行算法文本中给出的算法略有不同,在该算法中,最后一个元素切换到第一个元素,然后进行了分区。 这可能会改变排序的顺序,从而影响比较次数。

例如,您的代码:

def partition(A,start,end):
    pivot=A[end]
    pivot_index=start
    for i in range(start,end):
        if A[i] < pivot:
            swap(A,i,pivot_index)
            pivot_index+=1
    swap(A,pivot_index,end)
    return pivot_index

可以切换到:

def partition(A,start,end):
    swap(A,start,end)
    pivot=A[start]
    pivot_index=start + 1
    for i in range(start+1,end+1):
        if A[i] < pivot:
            swap(A,i,pivot_index)
            pivot_index+=1
    swap(A,pivot_index-1,start)
    return pivot_index-1

根据OP的评论进行编辑。

我建议您在脚本顶部声明全局变量,请查看以下工作示例:

inside = 0
def qsort(l):
    global inside
    inside += 1
    if len(l) <= 1:
        return l
    pivot = l[-1]
    return qsort(filter(lambda x: x < pivot, l[:-1])) + [pivot] + qsort(filter(lambda x: x >= pivot, l[:-1]))

import random
l = [random.randint(0,100) for _ in xrange(100)]
print qsort(l)
print inside

>>> [1, 1, 2, 3, 7, 9, 10, 11, 11, 11, 13, 13, 13, 13, 17, 17, 17, 18, 18, 21, 22, 23, 26, 26, 28, 30, 30, 32, 32, 34, 35, 38, 40, 41, 42, 42, 42, 42, 43, 44, 45, 46, 47, 47, 48, 48, 49, 51, 51, 54, 55, 56, 56, 56, 58, 59, 60, 60, 61, 61, 62, 63, 65, 67, 67, 68, 68, 70, 70, 72, 73, 74, 77, 79, 80, 83, 85, 85, 85, 86, 87, 89, 90, 90, 90, 91, 91, 95, 96, 96, 96, 97, 97, 97, 98, 98, 98, 99, 99, 99]
>>> 135

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM