Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Lines changed: 23 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /