
A sorted array using binary search
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).
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 raisedWhen #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
Add to your Gemfile
gem 'sorted_array_binary', github: 'FranckyU/sorted_array_binary', ref: "6a8dd894e379d4d4cbb674aa8ce45feb5a6d2315"
- Old syntax updates -> enforcing parenthesises around inner method calls (Elixir I miss your pipes!)
- Quieting 2.7 related warning around
SortedArrayBinary#initializesuper()when a block is passed.
