FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Interview-Question/Sorting/BubbleSort.js at master · namitmalasi/JavaScript-Interview-Question · GitHub
namitmalasi
/
JavaScript-Interview-Question
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Interview-Question
/
Sorting
/
BubbleSort.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
21 lines (17 loc) · 621 Bytes
Breadcrumbs
JavaScript-Interview-Question
/
Sorting
/
BubbleSort.js
Copy path
File metadata and controls
21 lines (17 loc) · 621 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
// In Bubble Sort algorithm,
// 1. traverse from left and compare adjacent elements and the higher one is placed at right side.
// 2. In this way, the largest element is moved to the rightmost end at first.
// 3. This process is then continued to find the second largest and place it and so on until
// the data is sorted.
function
BubbleSort
(
arr
)
{
let
n
=
arr
.
length
;
for
(
let
i
=
0
;
i
<
n
;
i
++
)
{
for
(
let
j
=
0
;
j
<
n
-
i
-
1
;
j
++
)
{
if
(
arr
[
j
]
<
arr
[
j
+
1
]
)
{
[
arr
[
j
]
,
arr
[
j
+
1
]
]
=
[
arr
[
j
+
1
]
,
arr
[
j
]
]
;
}
}
}
return
arr
;
}
console
.
log
(
BubbleSort
(
[
5
,
-
3
,
40
,
-
2
,
1
,
0
]
)
)
;
Back
|
FazBrowse Home
|
New Git URL