Skip to content

Commit

Permalink
solved: insertion_sort, try #2 -@iamserda
Browse files Browse the repository at this point in the history
  • Loading branch information
iamserda committed Feb 15, 2025
1 parent f998645 commit 6f54693
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions neetcodeio/algostructybeginners/Lv3-Sorting/insertion_sort2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ def insertionSort(self, arr: list) -> list:
for i in range(1, len(arr)):
j = i - 1
while j >= 0 and arr[j] > arr[j+1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
j -= 1
return arr



sol = Solution()

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

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

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

0 comments on commit 6f54693

Please sign in to comment.