FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
javascript/algorithms/sorting/insertionSort.js at master · AllAlgorithms/javascript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
AllAlgorithms
/
javascript
Public archive
Notifications
You must be signed in to change notification settings
Fork
43
Star
89
Code
Issues
0
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
javascript
/
algorithms
/
sorting
/
insertionSort.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
18 lines (16 loc) · 658 Bytes
Breadcrumbs
javascript
/
algorithms
/
sorting
/
insertionSort.js
Copy path
File metadata and controls
18 lines (16 loc) · 658 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
function
insertionSort
(
items
)
{
for
(
var
i
=
0
;
i
<
items
.
length
;
i
++
)
{
let
value
=
items
[
i
]
// store the current item value so it can be placed right
for
(
var
j
=
i
-
1
;
j
>
-
1
&&
items
[
j
]
>
value
;
j
--
)
{
// loop through the items in the sorted array (the items from the current to the beginning)
// copy each item to the next one
items
[
j
+
1
]
=
items
[
j
]
}
// the last item we've reached should now hold the value of the currently sorted item
items
[
j
+
1
]
=
value
}
return
list
}
const
list
=
[
54
,
26
,
93
,
17
,
77
,
31
,
44
,
55
,
20
]
console
.
log
(
insertionSort
(
list
)
)
// [ 17, 20, 26, 31, 44, 54, 55, 77, 93 ]
Back
|
FazBrowse Home
|
New Git URL