forked from Jack-Lee-Hiter/AlgorithmsByPython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.py
More file actions
Latest commit
25 lines (20 loc) · 667 Bytes
/
Copy pathInsertionSort.py
File metadata and controls
25 lines (20 loc) · 667 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
definsertionSort(alist):
forkey, iteminenumerate(alist):
index=key
whileindex>0andalist[index-1] >item:
alist[index] =alist[index-1]
index-=1
alist[index] =item
returnalist
alist= [54,26,93,17,77,31,44,55,20]
print(insertionSort(alist))
definsertionSort2(alist):
forindexinrange(1, len(alist)):
currentvalue=alist[index]
position=index
whileposition>0andalist[position-1] >currentvalue:
alist[position] =alist[position-1]
position-=1
alist[position] =currentvalue
returnalist
print(insertionSort2(alist))