changing-array-to-list

This commit is contained in:
vignesh
2018-04-14 08:35:58 +05:30
parent d3c9524379
commit 841385b5ed
9 changed files with 45 additions and 45 deletions

View File

@ -3,19 +3,19 @@
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 insertion_sort(arr):
def insertion_sort(lst):
for i in range(1, len(arr)):
key = arr[i]
for i in range(1, len(lst)):
key = lst[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
while j >= 0 and key < lst[j]:
lst[j + 1] = lst[j]
j -= 1
arr[j + 1] = key
lst[j + 1] = key
```
```python
arr = [7,4,9,2,6,3]
insertionsort(arr)
print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9]
lst = [7,4,9,2,6,3]
insertionsort(lst)
print('Sorted %s' %lst) # sorted [2, 3, 4, 6, 7, 9]
```