functionthreeNumberSort(arr, order) {let pointer =0;for (consttargetof order) {for (let i = pointer; i <arr.length; i++) {constcurr= arr[i];if (target !== curr) continue;swap(arr, pointer, i); pointer++; } }return arr;}functionswap(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
functionthreeNumberSort(arr, order) {constpointers= [0,0,0];for (let i =0; i <arr.length; i++) {constcurr= arr[i];if (curr === order[0]) { pointers[0]++; pointers[1]++; pointers[2]++; } elseif (curr === order[1]) { pointers[1]++; pointers[2]++; } else { pointers[2]++; } }for (let i =0; i <arr.length; i++) {if (i < pointers[0]) { arr[i] = order[0]; } elseif (i < pointers[1]) { arr[i] = order[1]; } else { arr[i] = order[2]; } }return arr;}functionswap(arr, a, b) { [arr[b], arr[a]] = [arr[a], arr[b]];return arr;}// Do not edit the line below.exports.threeNumberSort = threeNumberSort;
3rd try
iteration - in place swap
functionthreeNumberSort(arr, order) {let f =0;let s =0;let e =arr.length-1;while (s <= e) {constcurr= arr[s];if (curr === order[0]) {swap(arr, f, s); f++; s++; } elseif (curr === order[1]) { s++; } else {swap(arr, e, s); e--; } }return arr;}functionswap(arr, a, b) { [arr[a], arr[b]] = [arr[b], arr[a]];return arr;}// Do not edit the line below.exports.threeNumberSort = threeNumberSort;