code
class InsertSort{
constructor(a){
this.a = a
}
Sort(){
let self = this
for (var i = 1; i < self.a.length; i++) {
let j = i ;
let target = self.a[i]
while (j>0&&target>self.a[j-1]) {
self.a[j] = self.a[j-1]
j--
}
self.a[j] = target
console.log(self.a)
}
return self.a
}
}
module.exports = InsertSort;
const Insert = require("./insertSort");
let a = [3,12,7,90,4,56];
const sort = new Insert(a);
result = sort.Sort();
console.log(result);
result
➜ insert node test.js
[ 12, 3, 7, 90, 4, 56 ]
[ 12, 7, 3, 90, 4, 56 ]
[ 90, 12, 7, 3, 4, 56 ]
[ 90, 12, 7, 4, 3, 56 ]
[ 90, 56, 12, 7, 4, 3 ]