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