Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4273e09

Browse files
committed
Update InsertionSort.py
1 parent c370925 commit 4273e09

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

PythonDataStructsAndAlgo/Array_Based_Seqs/InsertionSort.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Insertion sort runs at O(n^2) due to the nested loops
2+
for an unordered list."""
13
def insertion_Sort(A):
24
"""Sort list of comparable elements into ascending order"""
35

@@ -7,8 +9,9 @@ def insertion_Sort(A):
79
#while A[j-1] > than curr (A[j]) we swap leftwards until A[j-1] <curr
810
while j >0 and A[j-1]>curr: #element A[j-1] must be after current
911
A[j]=A[j-1]
10-
j-=1
11-
A[j]=curr #current is now in the right place
12+
j-=1 #current index that was swapped that will hold the current element we used for comparison
13+
A[j]=curr #current goes now to the right index
14+
#And we iterate the loop with again with the next kth element.
1215

1316
return A
1417

0 commit comments

Comments
 (0)