From 40fcedeb92750889ae3e4fa5b4c47afe8baddaea Mon Sep 17 00:00:00 2001 From: CodeHiestIndia <73088300+CodeHiestIndia@users.noreply.github.com> Date: Sun, 10 Oct 2021 11:47:03 +0530 Subject: [PATCH] Create insertionSort.py --- Python/insertionSort.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Python/insertionSort.py diff --git a/Python/insertionSort.py b/Python/insertionSort.py new file mode 100644 index 00000000..dc06a89f --- /dev/null +++ b/Python/insertionSort.py @@ -0,0 +1,23 @@ +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 + + +# Driver code to test above +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +print ("Sorted array is:") +for i in range(len(arr)): + print ("%d" %arr[i])