项目作者: mourner

项目描述 :
A fast selection algorithm in JavaScript.
高级语言: JavaScript
项目地址: git://github.com/mourner/quickselect.git
创建时间: 2016-02-17T12:42:17Z
项目社区:https://github.com/mourner/quickselect

开源协议:ISC License

下载


quickselect Node Simply Awesome

A tiny and fast selection algorithm in JavaScript
(specifically, Floyd-Rivest selection).

  1. quickselect(array, k[, left, right, compareFn]);

Rearranges items so that all items in the [left, k] are the smallest.
The k-th element will have the (k - left + 1)-th smallest value in [left, right].

  • array: the array to partially sort (in place)
  • k: middle index for partial sorting (as defined above)
  • left: left index of the range to sort (0 by default)
  • right: right index (last index of the array by default)
  • compareFn: compare function

Example:

  1. const arr = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39];
  2. quickselect(arr, 8);
  3. // arr is [39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]
  4. // ^^ middle index