Problems178
Search for a command to run...
Build sorted prefix by inserting each element into its correct slot
Try your own input
Change the array and replay the algorithm from step one.
Current step
Insertion Sort: build a sorted sub-array by inserting each element into its correct position.
Insertion Sort
1function insertionSort(arr) {//n: 8i: —key: —j: —2 for (let i = 1; i < arr.length; i++) {3 const key = arr[i];4 let j = i - 1;5 while (j >= 0 && arr[j] > key) {6 arr[j + 1] = arr[j];7 j--;8 }9 arr[j + 1] = key;10 }11 return arr;12}