Skip to content

Commit e7262c1

Browse files
committed
solved: insertion_sort 4x -@iamserda
1 parent 4158203 commit e7262c1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random
2+
3+
4+
class Solution:
5+
6+
def insertionSort(self, arr: list) -> list:
7+
if len(arr) < 2:
8+
return arr
9+
for idx in range(len(arr)):
10+
jdx = idx - 1
11+
while jdx >= 0 and arr[jdx] > arr[jdx + 1]:
12+
arr[jdx], arr[jdx + 1] = arr[jdx + 1], arr[jdx]
13+
jdx -= 1
14+
return arr
15+
16+
17+
sol = Solution()
18+
19+
arr = [5, 4, 3, 2, 1]
20+
assert sol.insertionSort(arr) == [1, 2, 3, 4, 5]
21+
22+
# arr = [10, 1, 20, 2, 30, 3, 4, 40, 5]
23+
# assert sol.insertionSort(arr) == [1, 2, 3, 4, 5, 10, 20, 30, 40]
24+
25+
# arr = [0, 4, 3, 2, 4, 0]
26+
# assert sol.insertionSort(arr) == [0, 0, 2, 3, 4, 4]
27+
28+
# arr = [1, 3, 5, 4, 2]
29+
# assert sol.insertionSort(arr) == [1, 2, 3, 4, 5]

0 commit comments

Comments
 (0)