We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c370925 commit 4273e09Copy full SHA for 4273e09
PythonDataStructsAndAlgo/Array_Based_Seqs/InsertionSort.py
@@ -1,3 +1,5 @@
1
+"""Insertion sort runs at O(n^2) due to the nested loops
2
+for an unordered list."""
3
def insertion_Sort(A):
4
"""Sort list of comparable elements into ascending order"""
5
@@ -7,8 +9,9 @@ def insertion_Sort(A):
7
9
#while A[j-1] > than curr (A[j]) we swap leftwards until A[j-1] <curr
8
10
while j >0 and A[j-1]>curr: #element A[j-1] must be after current
11
A[j]=A[j-1]
- j-=1
- 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.
15
16
return A
17
0 commit comments