FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Interview-Question/Sorting/SelectionSort.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
/
SelectionSort.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
21 lines (18 loc) · 643 Bytes
Breadcrumbs
JavaScript-Interview-Question
/
Sorting
/
SelectionSort.js
Copy path
File metadata and controls
21 lines (18 loc) · 643 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
// This algorithm repeatedly selects the smallest (or largest) element from the unsorted portion
// of the list and swaps it with the first element of the unsorted part. This process is repeated
// for the remaining unsorted portion until the entire list is sorted.
function
SelectionSort
(
arr
,
length
)
{
for
(
let
i
=
0
;
i
<
length
;
i
++
)
{
let
minIndex
=
i
;
for
(
let
j
=
i
+
1
;
j
<
length
;
j
++
)
{
if
(
arr
[
j
]
>
arr
[
minIndex
]
)
{
minIndex
=
j
;
}
}
if
(
minIndex
!==
i
)
{
[
arr
[
i
]
,
arr
[
minIndex
]
]
=
[
arr
[
minIndex
]
,
arr
[
i
]
]
;
}
}
return
arr
;
}
console
.
log
(
SelectionSort
(
[
64
,
25
,
12
,
22
,
11
]
,
5
)
)
;
Back
|
FazBrowse Home
|
New Git URL