2016-08-17 122 views
0

我已经成功测试了我的代码。它以最后一个元素作为关键点。 但是,当我尝试数总数没有。所做的比较,它显示不正确的计数。 我在计算通过全局变量tot_comparisons以最后一个元素为转轴快速排序python

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

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 
+0

只是想你的代码,不工作,'指数超出范围exception' – Netwave

+0

工作成功地在我结束.. 160361是tot_comparisons输出使用我的代码以上 – fsociety

回答

2

我检查了你的作品快速排序,尽管它在流行的算法给出文本的算法,其中最后一个元素被切换到第一,然后随之而来的划分略有不同。这可能会改变对比较次数有影响的排序顺序。

例如,你的代码:基于由OP评论

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 

编辑:

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 

可以切换到。

+0

我通过全局变量tot_comparisons – fsociety

+0

计数根据您的评论更新我的答案。让我知道它是否有效?我发现你的计数器对我来说工作正常,并且结果也起作用,所以差别必须在元素的排序中。 –

+0

是的,它的工作原理...所以要理解:你已经再次使用数组的第一个元素作为枢轴(但只有在与最后一个元素交换后)......这确保最后一个元素实际上用作枢轴。这是否正确的理解? – fsociety

0

我建议你在你的脚本的顶部声明全局变量,检查此工作基础的例子:

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 
+0

目标是不要得到工作代码..它是要了解我哪里错了 – fsociety