From 3e51b6d5ccd5d22019223f6fdc941e6d88bb2f93 Mon Sep 17 00:00:00 2001 From: TheWiseNerd-l <69200242+TheWiseNerd-l@users.noreply.github.com> Date: Wed, 21 Oct 2020 16:58:18 +0530 Subject: [PATCH] Added Insertion Sort --- InsertionSort.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 InsertionSort.py diff --git a/InsertionSort.py b/InsertionSort.py new file mode 100644 index 0000000..621ec19 --- /dev/null +++ b/InsertionSort.py @@ -0,0 +1,19 @@ +def InSort(ar): + #Assuming first element is sorted + #start on second element + for i in range(1, len(ar)): + ins = ar[i] + #Save the previous value + j = i-1 + #Move all the sorted larger elements ahead + while j>=0 and ar[j] > ins: + ar[j+1] = ar[j] + j = j-1 + #Make insertion XD + ar[j+1] = ins + + return ar + +#Print +print(InSort([5,6,8,9,2,3,66,45,1,25,00,-1])) +