From 0217b426cb48364275d0dd8c98a3830283619bef Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Tue, 20 Feb 2018 15:07:03 +0530 Subject: [PATCH] Update insertionSort.md --- snippets/insertionsort.md | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/snippets/insertionsort.md b/snippets/insertionsort.md index a18ecae40..cbf6c2442 100644 --- a/snippets/insertionsort.md +++ b/snippets/insertionsort.md @@ -1,13 +1,8 @@ -## Insertion Sort +### Insertion Sort On a very basic level, an insertion sort algorithm contains the logic of shifting around and inserting elements in order to sort an unordered list of any size. The way that it goes about inserting elements, however, is what makes insertion sort so very interesting! - -## Implementation ```python -arr = [] # list to sort - - def insertionsort(arr): for i in range(1, len(arr)): @@ -19,22 +14,8 @@ def insertionsort(arr): arr[j+1] = key ``` - -### Example ```python arr = [7,4,9,2,6,3] - -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 - - insertionsort(arr) print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9] ```