update insertion_sort
This commit is contained in:
31
README.md
31
README.md
@ -29,6 +29,7 @@
|
||||
<li><a href = "#deep_flatten"><code>deep_flatten</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 = "#insertion_sort"><code>insertion_sort</code></a></li>
|
||||
<li><a href = "#shuffle"><code>shuffle</code></a></li>
|
||||
<li><a href = "#spread"><code>spread</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>
|
||||
|
||||
### 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
|
||||
|
||||
:information_source: The same algorithm is already implemented via `random.shuffle`.
|
||||
|
||||
@ -23,4 +23,3 @@ is_lower_case:[Rohit Tanwar](@kriadmin)
|
||||
count_by:[Rohit Tanwar](@kriadmin)
|
||||
insertion_sort:[Meet Zaveri](@meetzaveri)
|
||||
difference_by:[Rohit Tanwar](@kriadmin)
|
||||
|
||||
|
||||
@ -7,11 +7,11 @@ 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 = i - 1
|
||||
while j >= 0 and key < arr[j]:
|
||||
arr[j + 1] = arr[j]
|
||||
j -= 1
|
||||
arr[j+1] = key
|
||||
arr[j + 1] = key
|
||||
```
|
||||
|
||||
```python
|
||||
|
||||
Reference in New Issue
Block a user