def insert(A,i): value = A[i] j = i while j != 0 and A[j-1]>value: A[j] = A[j-1] j = j - 1 A[j] = value def insertion_sort(A): for i in range(len(A)): insert(A, i) if __name__=="__main__": A = [7,3,5,2] print A insertion_sort(A) print A
Output:
[7, 3, 5, 2] [2, 3, 5, 7]
0 comments:
Post a Comment