Skip to content

Commit

Permalink
wip: attempting insertion sort 24h 48h later to make sure I understan…
Browse files Browse the repository at this point in the history
…d it -@iamserda
  • Loading branch information
iamserda committed Feb 15, 2025
1 parent 2a27fb9 commit f998645
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions neetcodeio/algostructybeginners/Lv3-Sorting/insertion_sort2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import random


class Solution:

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


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 = [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 f998645

Please sign in to comment.