From 577cbfcd54e0a00671e6c40a1fcf554c6449f252 Mon Sep 17 00:00:00 2001 From: Ali Jafari <50498845+alijafari79@users.noreply.github.com> Date: Thu, 3 Nov 2022 00:11:35 +0330 Subject: [PATCH] Create insertion_sort.py --- Python/insertion_sort.py | 23 +++++++++++++++++++++++ 1 file changed, 23 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..e73e30b --- /dev/null +++ b/Python/insertion_sort.py @@ -0,0 +1,23 @@ + +# Function to do insertion sort +def insertionSort(arr): + + # Traverse through 1 to len(arr) + for i in range(1, len(arr)): + + key = arr[i] + + # Move elements of arr[0..i-1], that are + # greater than key, to one position ahead + # of their current position + j = i-1 + while j >= 0 and key < arr[j] : + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + + +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +for i in range(len(arr)): + print ("% d" % arr[i])