From 4f4bce9c2e56fe2d6ced87b3a790199b7de67bfd Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Tue, 20 Feb 2018 15:03:22 +0530 Subject: [PATCH] Update insertionsort.md --- snippets/insertionsort.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/snippets/insertionsort.md b/snippets/insertionsort.md index bc5f9747c..ce5a11ec1 100644 --- a/snippets/insertionsort.md +++ b/snippets/insertionsort.md @@ -1,5 +1,23 @@ -### Insertion Sort +## Insertion Sort +## Implementation +```python +arr = [] # list to sort + + +def insertionsort(arr): + + for i in range(1, len(arr)): + key = arr[i] + j = i-1 + while j>=0 and key < arr[j]: + arr[j+1] = arr[j] + j -= 1 + arr[j+1] = key +``` + + +### Example ```python arr = [7,4,9,2,6,3]