Merge pull request #6 from meetzaveri/patch-1

Create insertionsort.md
This commit is contained in:
Rohit Tanwar
2018-02-20 16:52:44 +05:30
committed by GitHub
3 changed files with 26 additions and 2 deletions

View File

@ -21,4 +21,6 @@ palindrome:[Rohit Tanwar](@kriadmin)
is_upper_case:[Rohit Tanwar](@kriadmin)
is_lower_case:[Rohit Tanwar](@kriadmin)
count_by:[Rohit Tanwar](@kriadmin)
insertion_sort:[Meet Zaveri](@meetzaveri)
difference_by:[Rohit Tanwar](@kriadmin)

View File

@ -0,0 +1,21 @@
### 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!
```python
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
```
```python
arr = [7,4,9,2,6,3]
insertionsort(arr)
print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9]
```

View File

@ -22,3 +22,4 @@ is_upper_case:string
is_lower_case:string
count_by:list
difference_by:list
insertion_sort:list