Skip to content

Commit

Permalink
solved: insertion_sort 4x -@iamserda
Browse files Browse the repository at this point in the history
  • Loading branch information
iamserda committed Feb 23, 2025
1 parent 4158203 commit e7262c1
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions neetcodeio/algostructybeginners/Lv3-Sorting/insertion_sort4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import random


class Solution:

def insertionSort(self, arr: list) -> list:
if len(arr) < 2:
return arr
for idx in range(len(arr)):
jdx = idx - 1
while jdx >= 0 and arr[jdx] > arr[jdx + 1]:
arr[jdx], arr[jdx + 1] = arr[jdx + 1], arr[jdx]
jdx -= 1
return arr


sol = Solution()

arr = [5, 4, 3, 2, 1]
assert sol.insertionSort(arr) == [1, 2, 3, 4, 5]

# arr = [10, 1, 20, 2, 30, 3, 4, 40, 5]
# assert sol.insertionSort(arr) == [1, 2, 3, 4, 5, 10, 20, 30, 40]

# arr = [0, 4, 3, 2, 4, 0]
# assert sol.insertionSort(arr) == [0, 0, 2, 3, 4, 4]

# arr = [1, 3, 5, 4, 2]
# assert sol.insertionSort(arr) == [1, 2, 3, 4, 5]

0 comments on commit e7262c1

Please sign in to comment.