循环排序是一种就地排序,它基于您正在排序的排列可以分解为循环的想法。如果您将每个循环旋转一个位置,则数组将被排序。这可以很容易地进行编码,以便对数组的写入次数是任何就地排序所需的理论最小值(这对于例如闪存驱动器上的大型数据集来说非常好,您希望在其中最大限度地减少写入次数设备)。
有什么方法可以提高Wikipedia 上代码的运行时间,同时保持其就地排序并保持最佳写入次数,或者它是最好的?
这是实现(注意range(a, b)
从a
到b - 1
):
# Sort an array in place and return the number of writes.
def cycleSort(array):
writes = 0
# Loop through the array to find cycles to rotate.
for cycleStart in range(0, len(array) - 1):
item = array[cycleStart]
# Find where to put the item.
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
if array[i] < item:
pos += 1
# If the item is already there, this is not a cycle.
if pos == cycleStart:
continue
# Otherwise, put the item there or right after any duplicates.
while item == array[pos]:
pos += 1
array[pos], item = item, array[pos]
writes += 1
# Rotate the rest of the cycle.
while pos != cycleStart:
# Find where to put the item.
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
if array[i] < item:
pos += 1
# Put the item there or right after any duplicates.
while item == array[pos]:
pos += 1
array[pos], item = item, array[pos]
writes += 1
return writes