Git Product home page Git Product logo

quickselect's Introduction

quickselect Node Simply Awesome

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

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:

const arr = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39];

quickselect(arr, 8);

// arr is [39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]
//                                         ^^ middle index

quickselect's People

Contributors

deniscarriere avatar gswalden avatar hanzhi713 avatar mourner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

quickselect's Issues

pkg.main, pkg.module are incompatible, so require("quickselect") behaves differently under Rollup/Webpack

In Node, require("quickselect") returns the function quickselect, but in Webpack, require("quickselect") returns a module { default: quickselect }. This means a quickselect dependent can’t be compatible with both environments without contortions.

$ echo 'console.log(require("quickselect"))' > src.js
$ npm i quickselect webpack webpack-cli
$ node src.js
[Function: quickselect]
$ npx webpack -d
$ node dist/main.js 
Object [Module] { default: [Getter] }

This is because Node is using pkg.main (quickselect.js) and Webpack is using pkg.module (index.js), and the two files do not provide compatible interfaces.

This problem is called out in the Rollup documentation:

Note: There are some tools such as Babel, TypeScript, Webpack, and @rollup/plugin-commonjs that are capable of resolving a CommonJS require(...) call with an ES module. If you are generating CommonJS output that is meant to be interchangeable with ESM output for those tools, you should always use named export mode. The reason is that most of those tools will by default return the namespace of an ES module on require where the default export is the .default property.

This was previously touched on in #6, but only in the context of the current quickselect in Webpack being incompatible with an old quickselect in Webpack (resolved by bumping the major version), not the current quickselect in Webpack being incompatible with the current quickselect in Node (still an issue).

Three potential solutions are:

  • use named export mode, so require("quickselect").default would work everywhere; or
  • add quickselect.default = quickselect for compatibility; or
  • remove pkg.module.

Export module version in JavaScript API

Hello,
For GPL compliance, we have to provide source code exactly as used in our program. This can be problematic to determine if quickselect is included in processed source code files.

It would be nice if quickselect provided a version number accessible from JavaScript, such as

var quickselectversion = ....

Please add a LICENSE.md

Hi @mourner! We are planning to use this and rbrush in Cesium. Does this use the same license as rbrush? Would you be able to add a LICENSE.md file here so we can link to it?

Thanks and hope to see you at the Mapbox conference and FOSS4G NA.

New release with typescript definitions

Is it possible to release a new version of this package or add a @types/quickselect? It seems that the types are already available but only in the master branch and not on npm

Thanks!

Unexpected behavior

Cases:

K is negative:

const quickselect = require('quickselect');

const numbers = [1, 2, 3, 4];
quickselect(numbers, -99999);

console.log(numbers); // [ 3, 4, 2, undefined, '-99999': 1 ]

K is greater than numbers.length:

const quickselect = require('quickselect');

const numbers = [1, 2, 3, 4];

quickselect(numbers, 99999);
console.log(numbers); // [ 3, 4, undefined, 1, <99995 empty items>, 2 ]

quickselect 1.1.0 export change breaks dependees

1.1.0 changed from module.exports to es6 export default.

This causes dependees that require quickselect as caret dependency in package.json and import it with a require(), like https://github.com/mourner/rbush, to import an object and fail when trying to call it as a function.

The resulting error with console.log() of quickselect from rbush/index.js
quickselect

Interestingly, we use babel-add-module-exports-plugin in build and it's not helping. Either way, the package shouldn't be forcing users to use the plugin. A change in how the module is exported warrants a major version bump.

The library name is a misnomer

This is a very neat library/function. I am particularly happy that you chose to implement Floyd-Rivest rather than introselect. As a nitpick, the name quickselect seems like a misnomer considering that it refers to a completely different algorithm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.