FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms-python/algorithms/sort/insertion_sort.py at master · Mu-L/algorithms-python · GitHub
Mu-L
/
algorithms-python
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms-python
/
algorithms
/
sort
/
insertion_sort.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
25 lines (20 loc) · 640 Bytes
Breadcrumbs
algorithms-python
/
algorithms
/
sort
/
insertion_sort.py
Copy path
File metadata and controls
25 lines (20 loc) · 640 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
def
insertion_sort
(
arr
,
simulation
=
False
):
""" Insertion Sort
Complexity: O(n^2)
"""
iteration
=
0
if
simulation
:
print
(
"iteration"
,
iteration
,
":"
,
*
arr
)
for
i
in
range
(
len
(
arr
)):
cursor
=
arr
[
i
]
pos
=
i
while
pos
>
0
and
arr
[
pos
-
1
]
>
cursor
:
# Swap the number down the list
arr
[
pos
]
=
arr
[
pos
-
1
]
pos
=
pos
-
1
# Break and do the final swap
arr
[
pos
]
=
cursor
if
simulation
:
iteration
=
iteration
+
1
print
(
"iteration"
,
iteration
,
":"
,
*
arr
)
return
arr
Back
|
FazBrowse Home
|
New Git URL