From 3baf9b9e8e389adeb1ca47ca567fbe7f1257627a Mon Sep 17 00:00:00 2001 From: Sameer Khan Date: Sat, 12 Oct 2019 03:28:49 +0530 Subject: [PATCH] Add Insertion Sort in Python --- Python/InsertionSort.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Python/InsertionSort.py diff --git a/Python/InsertionSort.py b/Python/InsertionSort.py new file mode 100644 index 0000000..f3c052f --- /dev/null +++ b/Python/InsertionSort.py @@ -0,0 +1,17 @@ +def InsertionSort(List): + for i in range(1, len(List)): + key = List[i] + j = i - 1 + while j >= 0 and key < List[j]: + List[j + 1] = List[j] + j -= 1 + List[j + 1] = key + print('The sorted list: \t', List) + +if __name__ == "__main__": + lst = [] + size = int(input("\nEnter size of the list: \t")) + for i in range(size): + elements = int(input("Enter the element: \t")) + lst.append(elements) + InsertionSort(lst) \ No newline at end of file