Three Number Sort ⇒ counting sort or radix sort ⇒ three pointer with in-place swap

  • n x m, 1

function threeNumberSort(arr, order) {

  let pointer = 0;
  for (const target of order) {
    
    for (let i = pointer; i < arr.length; i++) {
      const curr = arr[i];
      if (target !== curr) continue;
      swap(arr, pointer, i);
      pointer++;
    }
    
  }

  return arr;
}

function swap(arr, a, b) {
  [arr[b], arr[a]] = [arr[a], arr[b]];
  return arr;
}

// Do not edit the line below.
exports.threeNumberSort = threeNumberSort;
  • faster?

  • with only 3 numbers?

  • three pointers

  • n, 1

  • 3rd try

  • iteration - in place swap

Last updated