fork download
  1. // your code goes here
  2. function insertionSort(arr, n) {
  3. for(let i=1;i<n;i++) {
  4. let key = arr[i];
  5. let j = i-1;
  6.  
  7. while(j>=0 && arr[j]>key) {
  8. arr[j+1] = arr[j];
  9. j--;
  10. }
  11. arr[j+1] = key;
  12. }
  13. return arr;
  14. }
  15.  
  16. console.log(insertionSort([4, 6, 1, 3, 2], 5))
Success #stdin #stdout 0.04s 16428KB
stdin
Standard input is empty
stdout
1,2,3,4,6