From a2b865e558c6a42483f9b2fd0351345859a0bf3a Mon Sep 17 00:00:00 2001 From: Jayadev V Date: Fri, 25 Sep 2020 10:26:12 +0530 Subject: [PATCH 1/3] Create insertionsort.py static array insertion sort --- Sorting_Searching/insertionsort.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Sorting_Searching/insertionsort.py diff --git a/Sorting_Searching/insertionsort.py b/Sorting_Searching/insertionsort.py new file mode 100644 index 0000000..ffc242d --- /dev/null +++ b/Sorting_Searching/insertionsort.py @@ -0,0 +1,23 @@ +def insertion_sort(array): + + # We start from 1 since the first element is trivially sorted + for index in range(1, len(array)): + currentValue = array[index] + currentPosition = index + + # As long as we haven't reached the beginning and there is an element + # in our sorted array larger than the one we're trying to insert - move + # that element to the right + while currentPosition > 0 and array[currentPosition - 1] > currentValue: + array[currentPosition] = array[currentPosition -1] + currentPosition = currentPosition - 1 + + + # We have either reached the beginning of the array or we have found + # an element of the sorted array that is smaller than the element + # we're trying to insert at index currentPosition - 1. + # Either way - we insert the element at currentPosition + array[currentPosition] = currentValue +array = [4, 22, 41, 40, 27, 30, 36, 16, 42, 37, 14, 39, 3, 6, 34, 9, 21, 2, 29, 47] +insertion_sort(array) +print("sorted array: " + str(array)) From c02256e80e42b1f4fcd6fad1508002906cfa3f80 Mon Sep 17 00:00:00 2001 From: Jayadev V Date: Fri, 25 Sep 2020 10:31:07 +0530 Subject: [PATCH 2/3] Update insertionsort.py insertion sort done --- Sorting_Searching/insertionsort.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Sorting_Searching/insertionsort.py b/Sorting_Searching/insertionsort.py index ffc242d..5b7b164 100644 --- a/Sorting_Searching/insertionsort.py +++ b/Sorting_Searching/insertionsort.py @@ -5,19 +5,20 @@ def insertion_sort(array): currentValue = array[index] currentPosition = index - # As long as we haven't reached the beginning and there is an element - # in our sorted array larger than the one we're trying to insert - move - # that element to the right + while currentPosition > 0 and array[currentPosition - 1] > currentValue: array[currentPosition] = array[currentPosition -1] currentPosition = currentPosition - 1 - # We have either reached the beginning of the array or we have found - # an element of the sorted array that is smaller than the element - # we're trying to insert at index currentPosition - 1. - # Either way - we insert the element at currentPosition + array[currentPosition] = currentValue -array = [4, 22, 41, 40, 27, 30, 36, 16, 42, 37, 14, 39, 3, 6, 34, 9, 21, 2, 29, 47] +array = [] +n = int(input("Enter number of elements : ")) +for i in range(0, n): + ele = int(input()) + + array.append(ele) # adding the element + insertion_sort(array) print("sorted array: " + str(array)) From c9bb2260351b4d92f43b2c2a38869c69b0430bf4 Mon Sep 17 00:00:00 2001 From: Jayadev V Date: Fri, 25 Sep 2020 10:46:35 +0530 Subject: [PATCH 3/3] Update insertionsort.py insertion sort showing array in each step --- Sorting_Searching/insertionsort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sorting_Searching/insertionsort.py b/Sorting_Searching/insertionsort.py index 5b7b164..0cb7bff 100644 --- a/Sorting_Searching/insertionsort.py +++ b/Sorting_Searching/insertionsort.py @@ -13,6 +13,8 @@ def insertion_sort(array): array[currentPosition] = currentValue + print("array now : ") + print(array) array = [] n = int(input("Enter number of elements : ")) for i in range(0, n):