Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c99d39

Browse files
Update README.md
1 parent 59bad2e commit 9c99d39

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

‎Sorting Algorithms/Insertion Sort/README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,28 @@
44
- The first value is a sublist element and is sequential because it already has a single value.
55
- The remaining values ​​in this sub list are added by comparison.
66

7-
<img src="https://miro.medium.com/max/3204/1*5t5q_OLP-kGwQyblAN-nog.png" />
7+
```python
8+
def insertion_sort(arr):
9+
# for every index in array
10+
for i in range(1, len(arr)):
11+
# set current values and position
12+
currentvalue = arr[i]
13+
position = i
14+
15+
# sorted sublist
16+
while position > 0 and arr[position-1]>currentvalue:
17+
arr[position] = arr[position-1]
18+
position = position - 1
19+
20+
arr[position] = currentvalue
21+
return arr
22+
23+
arr = [3,2,13,4,6,5,7,8,1,20]
24+
print("Output: ", insertion_sort(arr))
25+
```
26+
output:
27+
```
28+
[1, 2, 3, 4, 5, 6, 7, 8, 13, 20]
29+
```
30+
31+
<img src="https://miro.medium.com/max/3204/1*5t5q_OLP-kGwQyblAN-nog.png" />

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /