Skip to content

Commit 918096d

Browse files
author
Muh. Angga Muttaqien
committed
add insertion sort
1 parent e42766b commit 918096d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

sorting/insertion-sort.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
def insertion_sort(collection):
3+
4+
length = len(collection)
5+
6+
for index in range(1, length):
7+
while 0 < index and collection[index] < collection[index-1]:
8+
collection[index], collection[index-1] = (
9+
collection[index-1], collection[index]
10+
)
11+
index -= 1
12+
13+
return collection
14+
15+
16+
def main():
17+
print("=== Insertion Sort (Algorithm) - An efficient, general-purpose, comparison-based sorting algorithm which most implementations produce a stable sort. Merge-sort is a divide and conquer algorithm")
18+
numbers = raw_input("Enter numbers separated by a comma: ")
19+
unsorted = [int(item) for item in numbers.split(',')]
20+
print(insertion_sort(unsorted))
21+
22+
if __name__=='__main__':
23+
main()

0 commit comments

Comments
 (0)