Python 就地快速排序实现
我正在尝试实现中解释的就地快速排序 下面是python代码,分区函数不能按预期工作Python 就地快速排序实现,python,algorithm,data-structures,Python,Algorithm,Data Structures,我正在尝试实现中解释的就地快速排序 下面是python代码,分区函数不能按预期工作 def swap(array, index1, index2): tmp = array[index1] array[index1] = array[index2] array[index2] = tmp def partition(array, left, right, pivotIndex): pivotValue = array[pivotIndex] swap(a
def swap(array, index1, index2):
tmp = array[index1]
array[index1] = array[index2]
array[index2] = tmp
def partition(array, left, right, pivotIndex):
pivotValue = array[pivotIndex]
swap(array, pivotIndex, right)
storeIndex = left
for i in range(left, right - 1):
if array[i] < pivotValue:
swap(array, i, storeIndex)
storeIndex = storeIndex + 1
print array, i
swap(array, storeIndex, right)
return storeIndex
def quicksort(array, left ,right):
if right > left:
print left, right
pivotIndex = left
pivotNewIndex = partition(array, left, right, pivotIndex)
quicksort(array, left, pivotNewIndex - 1)
quicksort(array, pivotNewIndex + 1, right)
if __name__ == '__main__':
array = [3,7,8,5,2,1,9,5,4]
partition(array, 0, len(array) - 1, 3) # 5 is pivot
print array # expecting all the elements to the left of pivot value(5) will be lesser or equal.
def交换(阵列、index1、index2):
tmp=数组[index1]
数组[index1]=数组[index2]
数组[index2]=tmp
def分区(数组、左、右、数据透视索引):
数据透视值=数组[数据透视索引]
交换(数组,数据透视索引,右侧)
storeIndex=左
对于范围内的i(左,右-1):
如果数组[i]<数据透视值:
交换(数组、i、storeIndex)
storeIndex=storeIndex+1
打印数组,i
交换(数组,storeIndex,右侧)
返回存储索引
def快速排序(数组、左、右):
如果右>左:
左、右打印
数据透视索引=左
pivotNewIndex=分区(数组、左、右、pivotIndex)
快速排序(数组,左,pivotNewIndex-1)
快速排序(数组,pivotNewIndex+1,右)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
数组=[3,7,8,5,2,1,9,5,4]
分区(数组,0,len(数组)-1,3)#5是枢轴
打印数组#希望轴值(5)左侧的所有元素都较小或相等。
您至少需要进行两次修复:
def partition(array, left, right, pivotIndex):
pivotValue = array[pivotIndex]
swap(array, pivotIndex, right)
storeIndex = left
for i in range(left, right): # range doesn't include right element already
if array[i] <= pivotValue: # need to check for equality (not really necessary for the sorting routine)
swap(array, i, storeIndex)
storeIndex = storeIndex + 1
print array, i
swap(array, storeIndex, right)
return storeIndex
def分区(数组、左、右、数据透视索引):
数据透视值=数组[数据透视索引]
交换(数组,数据透视索引,右侧)
storeIndex=左
对于范围内的i(左、右):#范围内尚未包含右元素
如果阵列[i]需要进行至少2次修复:
def partition(array, left, right, pivotIndex):
pivotValue = array[pivotIndex]
swap(array, pivotIndex, right)
storeIndex = left
for i in range(left, right): # range doesn't include right element already
if array[i] <= pivotValue: # need to check for equality (not really necessary for the sorting routine)
swap(array, i, storeIndex)
storeIndex = storeIndex + 1
print array, i
swap(array, storeIndex, right)
return storeIndex
def分区(数组、左、右、数据透视索引):
数据透视值=数组[数据透视索引]
交换(数组,数据透视索引,右侧)
storeIndex=左
对于范围内的i(左、右):#范围内尚未包含右元素
如果数组[i]使用更少的变量和干净的方法不是更好的方法吗
#!/usr/bin/python
Array = [1,2,3,4,5,4,3,23,4,5,4,3,2,1,2,3,4,3,4,1,412,2,4]
def swap(a,i,j):
temp=a[i]
a[i]=a[j]
a[j]=temp
def partition(a, left, right):
pivotIndex=right
for i in range(left,right):
if a[i] > a[pivotIndex]:
swap(a,i,pivotIndex)
return pivotIndex
def quicksort(array , left,right):
if left<right:
pivotIndex=partition(array,left,right)
quicksort(array, left, pivotIndex-1)
quicksort(array, pivotIndex+1, right)
def main():
quicksort(Array, 0 , len(Array) -1)
print (Array )
main()
Array=[1,2,3,4,5,4,3,23,4,5,4,4,3,2,1,2,3,4,3,4,1412,2,4]
def交换(a、i、j):
温度=a[i]
a[i]=a[j]
a[j]=温度
def分区(a、左、右):
数据透视索引=右
对于范围内的i(左、右):
如果a[i]>a[pivotIndex]:
交换(a、i、数据透视索引)
返回数据透视索引
def快速排序(数组、左、右):
如果左是否使用较少的变量和干净的方法不是更好的方法
#!/usr/bin/python
Array = [1,2,3,4,5,4,3,23,4,5,4,3,2,1,2,3,4,3,4,1,412,2,4]
def swap(a,i,j):
temp=a[i]
a[i]=a[j]
a[j]=temp
def partition(a, left, right):
pivotIndex=right
for i in range(left,right):
if a[i] > a[pivotIndex]:
swap(a,i,pivotIndex)
return pivotIndex
def quicksort(array , left,right):
if left<right:
pivotIndex=partition(array,left,right)
quicksort(array, left, pivotIndex-1)
quicksort(array, pivotIndex+1, right)
def main():
quicksort(Array, 0 , len(Array) -1)
print (Array )
main()
Array=[1,2,3,4,5,4,3,23,4,5,4,4,3,2,1,2,3,4,3,4,1412,2,4]
def交换(a、i、j):
温度=a[i]
a[i]=a[j]
a[j]=温度
def分区(a、左、右):
数据透视索引=右
对于范围内的i(左、右):
如果a[i]>a[pivotIndex]:
交换(a、i、数据透视索引)
返回数据透视索引
def快速排序(数组、左、右):
如果左键“未按预期工作”--预期工作内容是什么?它的作用是什么?尽量具体点。有时,在解决这些问题时,它会让你明白答案。你需要解释你期望它如何工作,以及它与你实际看到的有什么不同。如果你以错误的方式使用range
函数,请修复它(Maxim Skurydin的答案是正确的)。但一般来说,您可以编写更具python风格的代码,看看这些python中的快速排序示例:我有一个纯python和Cython中可用的快速排序:“没有按预期工作”——这是预期的?它的作用是什么?尽量具体点。有时,在解决这些问题时,它会让你明白答案。你需要解释你期望它如何工作,以及它与你实际看到的有什么不同。如果你以错误的方式使用range
函数,请修复它(Maxim Skurydin的答案是正确的)。但一般来说,您可以编写更python的代码,看看这些python中的快速排序示例:我在纯python和Cython中有一个可用的快速排序:我认为不需要检查相等性,是吗?这是正确的,它不应该影响整个排序算法。但是,OP在评论中提到,他希望轴值(5)左侧的所有元素都小于或等于。
我认为不需要检查相等性,是吗?这是正确的,不应该影响整体排序算法。但是,OP在评论中提到,他希望轴值(5)左侧的所有元素都小于或等于