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