首页 > 代码库 > 鸡尾酒排序

鸡尾酒排序


def cocktail_sort( arr ):
    arr_len = len( arr )
    half    = arr_len / 2
    for i in xrange( half ):
        for j in xrange( i, arr_len - i - 1 ):
            if arr[j] > arr[1 + j]:
               arr[j], arr[1 + j] = arr[1 + j], arr[j]
        for j in xrange( arr_len - i - 1, i, -1 ):
            if arr[j - 1] > arr[j]:
                arr[j - 1], arr[j] = arr[j], arr[j - 1]


if __name__ == "__main__":
    from random import randint
    
    arr = [ randint( 1, 110 ) for i in xrange( 100 ) ]
    print arr
    
    cocktail_sort( arr )
    print arr


鸡尾酒排序