update insertion_sort

This commit is contained in:
Rohit Tanwar
2018-02-20 17:08:10 +05:30
parent 9e7878b00d
commit 7cc78ca4c4
28 changed files with 1252 additions and 1222 deletions

View File

@ -29,6 +29,7 @@
<li><a href = "#deep_flatten"><code>deep_flatten</code></a></li> <li><a href = "#deep_flatten"><code>deep_flatten</code></a></li>
<li><a href = "#difference"><code>difference</code></a></li> <li><a href = "#difference"><code>difference</code></a></li>
<li><a href = "#difference_by"><code>difference_by</code></a></li> <li><a href = "#difference_by"><code>difference_by</code></a></li>
<li><a href = "#insertion_sort"><code>insertion_sort</code></a></li>
<li><a href = "#shuffle"><code>shuffle</code></a></li> <li><a href = "#shuffle"><code>shuffle</code></a></li>
<li><a href = "#spread"><code>spread</code></a></li> <li><a href = "#spread"><code>spread</code></a></li>
<li><a href = "#zip"><code>zip</code></a></li> <li><a href = "#zip"><code>zip</code></a></li>
@ -452,6 +453,36 @@ difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x
<br><a href = "#table-of-contents">:arrow_up: Back to top</a> <br><a href = "#table-of-contents">:arrow_up: Back to top</a>
### 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!
```py
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
```
<details><summary>View Examples</summary>
```py
arr = [7,4,9,2,6,3]
insertionsort(arr)
print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9]
```
</details>
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
### shuffle ### shuffle
:information_source: The same algorithm is already implemented via `random.shuffle`. :information_source: The same algorithm is already implemented via `random.shuffle`.

View File

@ -23,4 +23,3 @@ is_lower_case:[Rohit Tanwar](@kriadmin)
count_by:[Rohit Tanwar](@kriadmin) count_by:[Rohit Tanwar](@kriadmin)
insertion_sort:[Meet Zaveri](@meetzaveri) insertion_sort:[Meet Zaveri](@meetzaveri)
difference_by:[Rohit Tanwar](@kriadmin) difference_by:[Rohit Tanwar](@kriadmin)