Array insertion Sort Recursive - Node.js Algorithm

Node.js examples for Algorithm:Sort

Description

Array insertion Sort Recursive

Demo Code


function insertionSortRecursive(arr) {
  if (arr.length === 1) return arr;
  var last = arr[arr.length - 1];
  var left = arr.slice(0, arr.length - 1);
  left = insertionSortRecursive(left);//from   w  w w.  j ava 2s. c o m
  var i = left.length - 1;
  while (i >= 0 && left[i] > last) {
    left[i + 1] = left[i];
    i--;
  }
  left[i + 1] = last;
  return left;
}

Related Tutorials