forked from TamimEhsan/AlgorithmVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion-sort.js
More file actions
39 lines (37 loc) · 1.09 KB
/
Copy pathinsertion-sort.js
File metadata and controls
39 lines (37 loc) · 1.09 KB
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
36
37
38
39
// insertion-sort.js — Insertion sort algorithm with visualization step tracking
// Extracted from sortingAlgorithms.js for single-responsibility.
/**
* Insertion sort — builds sorted array one element at a time.
* Returns an array of step objects for animation.
* @param {Array} arr - Array of {width, ...} objects to sort
* @returns {Array} Array of {xx, yy, changed} step objects
*/
export function insertionSort(arr){
const pairs = [];
let n = arr.length;
const prevRect = arr.slice();
for (let i = 1; i < n; ++i) {
let key = prevRect[i].width;
let j = i - 1;
while (j >= 0 && prevRect[j].width > key) {
const recti = {...prevRect[j]};
const rectj = {...prevRect[j+1]};
prevRect[j+1] = recti;
prevRect[j] = rectj;
pairs.push( {
xx:j,
yy:j+1,
changed:true
} );
j = j - 1;
}
}
for(let i=0;i<n;i++){
pairs.push({
xx:i,
yy:i,
changed:true
})
}
return pairs;
}