-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.py
More file actions
27 lines (22 loc) · 873 Bytes
/
InsertionSort.py
File metadata and controls
27 lines (22 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""
Insertion sort is the simple method of sorting an array. In this technique,
the array is virtually split into the sorted and unsorted part.
An element from unsorted part is picked and is placed at correct position in the sorted part.
"""
class InsertionSort:
@staticmethod
def insertion_sort(unsorted_array):
for i in range(1, len(unsorted_array)):
k = unsorted_array[i]
j = i - 1
while j >= 0 and k < unsorted_array[j]:
unsorted_array[j + 1] = unsorted_array[j]
j -= 1
unsorted_array[j + 1] = k
return unsorted_array
if __name__ == "__main__":
insertion_sort_obj = InsertionSort()
list1 = input("Enter space separated unsorted values:").split()
list1 = list(map(int, list1))
result = insertion_sort_obj.insertion_sort(list1)
print(result)