From 8d2ef96ea77944555e153825fc794ae8e437a245 Mon Sep 17 00:00:00 2001 From: Leksono Nanto P Date: Wed, 28 Oct 2020 11:56:20 +0700 Subject: [PATCH 1/2] Create insertion_sort.py --- Python/insertion_sort.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Python/insertion_sort.py diff --git a/Python/insertion_sort.py b/Python/insertion_sort.py new file mode 100644 index 0000000..ceede57 --- /dev/null +++ b/Python/insertion_sort.py @@ -0,0 +1,21 @@ + +""" +Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. +So in beginning we compare the first two elements and sort them by comparing them. +Then we pick the third element and find its proper position among the previous two sorted elements. +This way we gradually go on adding more elements to the already sorted list by putting them in their proper position. +""" +def insertion_sort(InputList): + for i in range(1, len(InputList)): + j = i-1 + nxt_element = InputList[i] + # Compare the current element with next one + while (InputList[j] > nxt_element) and (j >= 0): + InputList[j+1] = InputList[j] + j=j-1 + InputList[j+1] = nxt_element + + +list = [19,2,31,45,30,11,121,27] +insertion_sort(list) +print(list) From 6545836b2df04c74f096f8277fd4e16044f15479 Mon Sep 17 00:00:00 2001 From: Leksono Nanto P Date: Wed, 28 Oct 2020 11:59:52 +0700 Subject: [PATCH 2/2] Update insertion_sort.py --- Python/insertion_sort.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Python/insertion_sort.py b/Python/insertion_sort.py index ceede57..84fd5b0 100644 --- a/Python/insertion_sort.py +++ b/Python/insertion_sort.py @@ -16,6 +16,6 @@ def insertion_sort(InputList): InputList[j+1] = nxt_element -list = [19,2,31,45,30,11,121,27] -insertion_sort(list) -print(list) +numbers = [19,2,31,45,30,11,121,27] +insertion_sort(numbers) +print(numbers)