Programming/Python

Sorting(3) Insertion Sort(삽입 정렬)

SiriusJ 2016. 10. 27. 19:43
반응형

파이썬 Insertion Sort로 오름차순으로 정렬하는 방식입니다.


시간복잡도 : O(n^2)


def insertionSort(A):

    for i in range(1, len(A)):

        temp = A[i]

        j = i

        while j > 0 and A[j-1] > temp:

            A[j] = A[j-1]

            j -= 1

        A[j] = temp


if __name__ == '__main__':

    A = [4, 1, 5, 8, 6, 2, 3, 7, 10]

    insertionSort(A)

    print A


반응형