美文网首页
Quick sort with equivalent eleme

Quick sort with equivalent eleme

作者: R0b1n_L33 | 来源:发表于2017-11-14 18:35 被阅读17次
def partition(array, p, r):
    x = array[r]
    j = i = p - 1
    for k in range(p, r):
        if array[k] < x:
            i += 1
            j += 1
            # Crucial: Assignment order must be rotative.
            array[k], array[j], array[i] = array[j], array[i], array[k]
        elif array[k] == x:
            j += 1
            array[j], array[k] = array[k], array[j]
    i += 1
    j += 1
    array[j], array[r] = array[r], array[j]
    return i, j

def quickSort(array, p, r):
    if p < r:
        i, j = partition(array, p, r)
        print('i j =', i, j)
        quickSort(array, p, i-1)
        quickSort(array, j+1, r)

相关文章

网友评论

      本文标题:Quick sort with equivalent eleme

      本文链接:https://www.haomeiwen.com/subject/snufvxtx.html