code: function mergeSort(arr) {
  
    /* merges two sorted arrays into one sorted array */
    function merge(a, b) {
  
      /* result set */
      var c = [];
  
      /* as long as there are elements in the arrays to be merged */
      while (a.length > 0 || b.length > 0) {
  
        /* are there elements to be merged, if yes, compare them and merge */
        var n = a.length > 0 && b.length > 0 ? a[0] < b[0] ? a.shift() : b.shift() : b.length > 0 ? b.shift() : a.length > 0 ? a.shift() : null;
  
        /* always push the smaller one onto the result set */
        if (n !== null) {
          c.push(n);
        }
      }
      return c;
    }
  
    /* this mergeSort implementation cuts the array in half, wich should be fine with randomized arrays, but introduces the risk of a worst-case scenario */
    median = Math.floor(arr.length / 2);
    var part1 = arr.slice(0, median); /* for some reason it doesn't work if inserted directly in the return statement (tried so with firefox) */
    var part2 = arr.slice(median - arr.length);
    return arr.length <= 1 ? arr : merge(mergeSort(part1), /* first half */
    mergeSort(part2) /* second half */
    );
  }
docs: /*
  Merge Sort:
  1. Cut the array in half
  2. Sort each of them recursively
  3. Merge the two sorted arrays
  4. An array with only one element is considered sorted