-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort.js
More file actions
29 lines (24 loc) · 811 Bytes
/
insertionsort.js
File metadata and controls
29 lines (24 loc) · 811 Bytes
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
//Pseudocode
/*----------------*/
let swap = (arr, index1, index2)=> [arr[index1], arr[index2]] = [arr[index2], arr[index1]];
let insertionSort = (arr)=>{
for (let i = 0; i < arr.length; i++){
if (arr[i] > arr[i+1]){
console.log(arr, arr[i] , arr[i+1]);
swap(arr, i, i+1);
console.log(arr, "Swapped 1")
for (let j = i - 1; j>= 0; j--){
if (arr[j] > arr[j+1]){
console.log(arr, arr[j], arr[j+1]);
console.log(arr, " Swarpped 2")
swap(arr, j, j+1);
} else {
console.log("Break")
break;
}
}
}
}
console.log(arr, " is the sorted array");
}
insertionSort([-1,1,2,4,3,-6]);