mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
ce7faa5a3a
* Insertion_sort * largest subarray sum * updated print command * removed extraspaces * removed sys.maxint * added explaination * Updated function style * Update largest_subarray_sum.py * Update i_sort.py * Delete bogo_bogo_sort.py
22 lines
426 B
Python
22 lines
426 B
Python
def insertionSort(arr):
|
|
"""
|
|
>>> a = arr[:]
|
|
>>> insertionSort(a)
|
|
>>> a == sorted(a)
|
|
True
|
|
"""
|
|
for i in range(1, len(arr)):
|
|
key = arr[i]
|
|
j = i - 1
|
|
while j >= 0 and key < arr[j]:
|
|
arr[j + 1] = arr[j]
|
|
j -= 1
|
|
arr[j + 1] = key
|
|
|
|
|
|
arr = [12, 11, 13, 5, 6]
|
|
insertionSort(arr)
|
|
print("Sorted array is:")
|
|
for i in range(len(arr)):
|
|
print("%d" % arr[i])
|