Giter Site home page Giter Site logo

upupming / quick-select Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 18 KB

Select k-smallest item in an array A in nearly 2n time using LAZY-SELECT. Also implemented naive sorting & linear select algorithm.

Python 100.00%
algorithm random-algorithm select-algorithm lazy-selection

quick-select's Introduction

min(A, k) algorithm

Author: Li Yiming

  • Sort select: O(n log n)
  • Linear select: 1.5n ~ 5.43n
  • Lazy select: 2n + o(n)

See my report (in Chinese) for more technical information.

Run test cases and see the logs under log folder (you have to manually create it if not exist):

python test/test.py

The log file with WARNING is a simple version of log, and DEBUG is a more verbose version of log.

For example, the lazy-WARNING.log file has only the summary of the algorithm result and time used:

min([ 4  7  2 10  3  3], 0) = 2
Time used: 0.0seconds
min([ 4  7  2 10  3  3], 1) = 3
Time used: 0.0seconds
min([ 4  7  2 10  3  3], 2) = 3
Time used: 0.0seconds
min([ 4  7  2 10  3  3], 3) = 4
Time used: 0.0seconds
min([ 4  7  2 10  3  3], 4) = 7
Time used: 0.0seconds
min([ 4  7  2 10  3  3], 5) = 10
Time used: 0.0seconds

But the lazy-DEBUG.log file contains all the steps taken during the algorithm, it will be useful for you to understand the algorithm quickly.

Benchmark

You can run benchmark with command, the output will be written to files under log folder:

python test/benchmark.py

Zipf distribution seems more difficult to select, so I only tested the case when the length of A is 10,000.

Distribution Algorithm Length of A Time used
Uniform SORT-SELECT (numpy) 1,000,000 0.24885845184326172seconds
Uniform LINEAR-SELECT 1,000,000 3.949763059616089seconds
Uniform LAZY-SELECT 1,000,000 4.454483985900879seconds
Normalize SORT-SELECT (numpy) 1,000,000 0.12593460083007812seconds
Normalize LINEAR-SELECT 1,000,000 0.5047116279602051seconds
Normalize LAZY-SELECT 1,000,000 2.378643274307251seconds
Zipf with ζ = 1.01 SORT-SELECT (numpy) 10,000 0.0010001659393310547seconds
Zipf with ζ = 1.01 LINEAR-SELECT 10,000 0.9494802951812744seconds
Zipf with ζ = 1.01 LAZY-SELECT 10,000 0.03298377990722656seconds

quick-select's People

Contributors

upupming avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.