칵테일 정렬 귀속python 실현

1296 단어 python
# coding=utf-8
__author__ = 'tsaowe'


# 
def sort(array, start, end):
    """
    :param array:  
    :param start:  
    :param end:  
    """

    if start > end:
        if abs(start - end) == 1:
            if array[start] < array[end]:
                array[start], array[end] = array[end], array[start]
        else:
            for i in range(start, end, -1):
                if array[i] < array[i - 1]:
                    array[i], array[i - 1] = array[i - 1], array[i]
            sort(array, end + 1, start)
    else:
        if abs(start - end) == 1:
            if array[start] > array[end]:
                array[start], array[end] = array[end], array[start]
        else:
            for i in range(start, end, 1):
                if array[i] > array[i + 1]:
                    array[i], array[i + 1] = array[i + 1], array[i]
            sort(array, end - 1, start)


if __name__ == '__main__':
    array = [1, 9, 7, 2, 6, 8, 10, -2, 7, 7, 89, 87, 45, -9]
    sort(array, 0, len(array) - 1)
    print(array)

좋은 웹페이지 즐겨찾기