FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-algorithms/algorithms/sort/bubble_sort.py at master · ilyvsc/python-algorithms · GitHub
ilyvsc
/
python-algorithms
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
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
python-algorithms
/
algorithms
/
sort
/
bubble_sort.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (26 loc) · 786 Bytes
Breadcrumbs
python-algorithms
/
algorithms
/
sort
/
bubble_sort.py
Copy path
File metadata and controls
35 lines (26 loc) · 786 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
26
27
28
29
30
31
32
33
34
35
"""
https://en.wikipedia.org/wiki/Bubble_sort
Worst-case performance: O(N^2)
If you call bubble_sort(arr,True), you can see the process of the sort
Default is simulation = False
"""
def
bubble_sort
(
arr
,
simulation
=
False
):
def
swap
(
i
,
j
):
arr
[
i
],
arr
[
j
]
=
arr
[
j
],
arr
[
i
]
n
=
len
(
arr
)
swapped
=
True
iteration
=
0
if
simulation
:
print
(
"iteration"
,
iteration
,
":"
,
*
arr
)
x
=
-
1
while
swapped
:
swapped
=
False
x
=
x
+
1
for
i
in
range
(
1
,
n
-
x
):
if
arr
[
i
-
1
]
>
arr
[
i
]:
swap
(
i
-
1
,
i
)
swapped
=
True
if
simulation
:
iteration
=
iteration
+
1
print
(
"iteration"
,
iteration
,
":"
,
*
arr
)
return
arr
Back
|
FazBrowse Home
|
New Git URL