FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm-visualizer/src/algorithms/insertionSort.ts at master · dev-grid/algorithm-visualizer · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
dev-grid
/
algorithm-visualizer
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
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
algorithm-visualizer
/
src
/
algorithms
/
insertionSort.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
31 lines (28 loc) · 928 Bytes
Breadcrumbs
algorithm-visualizer
/
src
/
algorithms
/
insertionSort.ts
Copy path
File metadata and controls
31 lines (28 loc) · 928 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
import
{
AnimationArrayType
}
from
"@/lib/types"
;
function
runInsertionSort
(
array
:
number
[
]
,
animations
:
AnimationArrayType
)
{
for
(
let
i
=
1
;
i
<
array
.
length
;
i
++
)
{
animations
.
push
(
[
[
i
]
,
false
]
)
;
const
currentValue
=
array
[
i
]
;
let
j
=
i
-
1
;
while
(
j
>=
0
&&
array
[
j
]
>
currentValue
)
{
animations
.
push
(
[
[
j
,
j
+
1
,
i
]
,
false
]
)
;
array
[
j
+
1
]
=
array
[
j
]
;
animations
.
push
(
[
[
j
+
1
,
array
[
j
]
]
,
true
]
)
;
j
-=
1
;
}
array
[
j
+
1
]
=
currentValue
;
animations
.
push
(
[
[
j
+
1
,
currentValue
]
,
true
]
)
;
}
}
export
function
generateInsertionSortAnimationArray
(
isSorting
:
boolean
,
array
:
number
[
]
,
runAnimation
:
(
animations
:
AnimationArrayType
)
=>
void
)
{
if
(
isSorting
)
return
;
if
(
array
.
length
<=
1
)
return
[
]
;
const
animations
:
AnimationArrayType
=
[
]
;
const
auxiliaryArray
=
array
.
slice
(
)
;
runInsertionSort
(
auxiliaryArray
,
animations
)
;
runAnimation
(
animations
)
;
}
Back
|
FazBrowse Home
|
New Git URL