Skip to content

About

Open Source Ruby Gem :: A faster sorted array data structure using binary search algorithm

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

 
 

Repository files navigation

sorted_array_binary

Build Status [![Coverage Status] (https://coveralls.io/repos/ledestin/sorted_array_binary/badge.png)] (https://coveralls.io/r/ledestin/sorted_array_binary) Code Climate

A sorted array using binary search

Why

Existing sorted array gems as of Jan 2014:

  • sorted_array (0.0.5)
  • array-sorted (1.1.2)

None of them use binary search in their implementations (i.e. slow or very slow).

Example

require 'sorted_array_binary'

# Use standard sorting via <=>.
array = SortedArrayBinary.new
array.push 'b', 'a' #=> ['a', 'b']

# Use custom sorting block.
array = SortedArrayBinary.new { |a, b| b <=> a }
array.push 'a', 'b' #=> ['b', 'a']

# Take care to only add items that can be compared with <=>.
array.push nil, 1 #=> exception is raised

Performance

When #push'ing 1000 random numbers into an array:

sorted_array        (0.0.5)     1.179088
array-sorted        (1.1.2)     0.076348
sorted_array_binary (0.0.3)     0.005244

Installation

Add to your Gemfile

gem 'sorted_array_binary', github: 'FranckyU/sorted_array_binary', ref: "6a8dd894e379d4d4cbb674aa8ce45feb5a6d2315"

2020 updates

  • Old syntax updates -> enforcing parenthesises around inner method calls (Elixir I miss your pipes!)
  • Quieting 2.7 related warning around SortedArrayBinary#initialize super() when a block is passed.

About

Open Source Ruby Gem :: A faster sorted array data structure using binary search algorithm

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages