求助帮忙看一下用python实现快速排序的代码为什么一直无限循环了?应该怎么修改呢
#!/usr/bin/envpython#-*-coding:utf-8-*-#coding=utf-8#下面的算法是快速排序,利用了二分排序的思想defquickSof...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#coding=utf-8
#下面的算法是快速排序,利用了二分排序的思想
def quickSoft(left,right,listParam):
if left>right:
return
temp = listParam[left]
i = left+1
j = right
while i!=j:
while listParam[j]>temp and i<j:
j -= 1
while listParam[i]<temp and i<j:
i += 1
if i<j:
t = listParam[i]
listParam[i]=listParam[j]
listParam[j]=t
listParam[left] = listParam[i]
listParam[i] = temp
print i,',',j
quickSoft(left,i-1,listParam)
quickSoft(i+1,right,listParam)
return listParam
listA = [6,1,2,7,9,3,4,5,10,8]
listB = quickSoft(0,len(listA)-1,listA)
print listB 展开
# -*- coding: utf-8 -*-
#coding=utf-8
#下面的算法是快速排序,利用了二分排序的思想
def quickSoft(left,right,listParam):
if left>right:
return
temp = listParam[left]
i = left+1
j = right
while i!=j:
while listParam[j]>temp and i<j:
j -= 1
while listParam[i]<temp and i<j:
i += 1
if i<j:
t = listParam[i]
listParam[i]=listParam[j]
listParam[j]=t
listParam[left] = listParam[i]
listParam[i] = temp
print i,',',j
quickSoft(left,i-1,listParam)
quickSoft(i+1,right,listParam)
return listParam
listA = [6,1,2,7,9,3,4,5,10,8]
listB = quickSoft(0,len(listA)-1,listA)
print listB 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询