Quicksort递归错误

时间:2017-02-18 05:39:49

标签: python recursion quicksort

在实施quicksort时我遇到了问题:     在这里输入代码

def partition(list,p,r):
  i=p-1
  j=p
  x=list[r]
  for j in range(p,r)://the program is working fine but if i replace it with only r its not working anymore,why is that p is always 0and the loop will run similarly
    if list[j]<list[r]:
        i=i+1
        temp=list[i]
        list[i]=list[j]
        list[j]=temp
 temp1=list[i+1]
 list[i+1]=x
 list[r]=temp1
 return i+1

def quicksort(list,p,r):
    if p<r:
     q=partition(list,p,r)
     quicksort(list,p,q-1)
     quicksort(list,q+1,r)
    return list

list = quicksort([5,3,1,6,7,9,8],0,6)   打印列表

它正在排序但是如果我改变了循环范围它的显示错误

0 个答案:

没有答案