-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #392 from mthakur7/patch-2
Create insertionSort.py
- Loading branch information
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
Program's_Contributed_By_Contributors/Python_Programs/insertionSort.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
def insertionSort(arr): | ||
|
||
# traversing through the length of whole array | ||
for i in range(1, len(arr)): | ||
key = arr[i] | ||
j = i-1 | ||
while j >=0 and key < arr[j] : | ||
#comparing current element with its previous one, and if previous one is greater | ||
#move it one place ahead of current | ||
arr[j+1] = arr[j] | ||
j -= 1 | ||
arr[j+1] = key | ||
|
||
|
||
# Driver test case | ||
arr = [12, 11, 13, 5, 6] | ||
insertionSort(arr) | ||
print ("Sorted array is:") | ||
for i in range(len(arr)): | ||
print ("%d" %arr[i]) |