How to use quickselect - 4 common examples

To help you get started, we’ve selected a few quickselect examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github awesome-schedule / plannable / src / algorithm / ScheduleEvaluator.ts View on Github external
public partialSort(arr: Uint32Array, compare: (x: number, y: number) => number, num: number) {
        const len = arr.length;

        // no point to use quick select if num of elements to be selected is greater than half of the length
        if (num >= len / 2) {
            arr.sort(compare);
        } else {
            quickselect(arr, num, 0, len - 1, compare);
            const slc = arr.slice(0, num).sort(compare);
            for (let i = 0; i < num; i++) arr[i] = slc[i];
        }
    }
github xiaoiver / custom-mapbox-layer / src / utils / classify-rings.ts View on Github external
if (polygon) polygons.push(polygon);
      polygon = [rings[i]];

    } else {
      // @ts-ignore
      polygon.push(rings[i]);
    }
  }
  if (polygon) polygons.push(polygon);

  // Earcut performance degrages with the # of rings in a polygon. For this
  // reason, we limit strip out all but the `maxRings` largest rings.
  if (maxRings > 1) {
    for (let j = 0; j < polygons.length; j++) {
      if (polygons[j].length <= maxRings) continue;
      quickselect(polygons[j], maxRings, 1, polygons[j].length - 1, compareAreas);
      polygons[j] = polygons[j].slice(0, maxRings);
    }
  }

  return polygons;
}
github mapbox / mapbox-gl-js / src / util / classify_rings.js View on Github external
if (ccw === area < 0) {
            if (polygon) polygons.push(polygon);
            polygon = [rings[i]];

        } else {
            (polygon: any).push(rings[i]);
        }
    }
    if (polygon) polygons.push(polygon);

    // Earcut performance degrades with the # of rings in a polygon. For this
    // reason, we limit strip out all but the `maxRings` largest rings.
    if (maxRings > 1) {
        for (let j = 0; j < polygons.length; j++) {
            if (polygons[j].length <= maxRings) continue;
            quickselect(polygons[j], maxRings, 1, polygons[j].length - 1, compareAreas);
            polygons[j] = polygons[j].slice(0, maxRings);
        }
    }

    return polygons;
}
github mourner / rbush / index.js View on Github external
function multiSelect(arr, left, right, n, compare) {
    const stack = [left, right];

    while (stack.length) {
        right = stack.pop();
        left = stack.pop();

        if (right - left <= n) continue;

        const mid = left + Math.ceil((right - left) / n / 2) * n;
        quickselect(arr, mid, left, right, compare);

        stack.push(left, mid, mid, right);
    }
}

quickselect

A tiny and fast selection algorithm in JavaScript.

ISC
Latest version published 4 months ago

Package Health Score

78 / 100
Full package analysis

Popular quickselect functions