-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.js
More file actions
32 lines (29 loc) · 1.16 KB
/
insertionSort.js
File metadata and controls
32 lines (29 loc) · 1.16 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
// On the evening of 26th August 2024
// a function to get the length of an iterable eg ARRAY, STRING e.t.c
function count (iterable){
if(!isNaN(iterable))iterable = `${iterable}`;
let i= 0;
for(const item of iterable) i++;
return i;
}
// the Insertion sort logic
function insertionSort(array){
let sorted= [], len = count(array), temp, swaps = 0, swapsCount= [], iterations = 0;
sorted[0] = array[0];
for(let i = 1; i<len; i++){
for(let j = 0; j<count(sorted); j++){
if(array[i]<sorted[j]){
[array[i], sorted[j]] = [sorted[j], array[i]];
swaps++;
}
}
sorted.push(array[i]);
swapsCount.push(swaps);
iterations ++;
}
return[sorted, `No of swaps per iteration ${swapsCount}`, `No of iterations made to complete the sort ${iterations}`]
}
console.log(insertionSort([22,3,44,5,6,8,7,10,9,15]));// normal list
console.log(insertionSort([1,3,2,4,4,5,6,7,8,9])); // list with only one misplaced element
console.log(insertionSort([13,12,11,9,8,7,6,5,3,2])); // a reverse-ordered list
console.log(insertionSort([10,11,22,33,44,55,66,77,88,99])); // a sorted list