diff --git a/lib/algorithmable.rb b/lib/algorithmable.rb index 771cdec..355fedc 100644 --- a/lib/algorithmable.rb +++ b/lib/algorithmable.rb @@ -14,6 +14,7 @@ module Algorithmable autoload :LevenshteinDistance, 'algorithmable/levenshtein_distance' autoload :Cups, 'algorithmable/cups' autoload :Cache, 'algorithmable/cache' + autoload :Trees, 'algorithmable/trees' autoload :UnionFind, 'algorithmable/union_find' class << self diff --git a/lib/algorithmable/data_structs/linked_list/doubly.rb b/lib/algorithmable/data_structs/linked_list/doubly.rb index 275079e..d846516 100644 --- a/lib/algorithmable/data_structs/linked_list/doubly.rb +++ b/lib/algorithmable/data_structs/linked_list/doubly.rb @@ -2,18 +2,6 @@ module Algorithmable module DataStructs module LinkedList class Doubly < Base - class Node - attr_accessor :item, :prev, :next, :front - - def initialize(item, prev_pointer = nil, next_pointer = nil) - @item = item - @prev = prev_pointer - @next = next_pointer - end - end - - private_constant :Node - def push_front(item) prev_front = @front node = new_node item, nil, prev_front @@ -80,6 +68,18 @@ def delete(item) def new_node(item, prev_pointer = nil, next_pinter = nil) Node.new item, prev_pointer, next_pinter end + + class Node + attr_accessor :item, :prev, :next, :front + + def initialize(item, prev_pointer = nil, next_pointer = nil) + @item = item + @prev = prev_pointer + @next = next_pointer + end + end + + private_constant :Node end end end diff --git a/lib/algorithmable/data_structs/linked_list/singly.rb b/lib/algorithmable/data_structs/linked_list/singly.rb index 449f83a..ac75fef 100644 --- a/lib/algorithmable/data_structs/linked_list/singly.rb +++ b/lib/algorithmable/data_structs/linked_list/singly.rb @@ -2,16 +2,6 @@ module Algorithmable module DataStructs module LinkedList class Singly < Base - class Node - attr_accessor :item, :next, :front - - def initialize(item, next_pointer = nil) - @item = item - @next = next_pointer - end - end - - private_constant :Node def push_front(obj) node = new_node(obj) @@ -182,6 +172,17 @@ def remove_next(prev) def new_node(item, next_pinter = nil) Node.new item, next_pinter end + + class Node + attr_accessor :item, :next, :front + + def initialize(item, next_pointer = nil) + @item = item + @next = next_pointer + end + end + + private_constant :Node end end end diff --git a/lib/algorithmable/data_structs/ordered_symbol_table.rb b/lib/algorithmable/data_structs/ordered_symbol_table.rb index 4fb5513..06413b9 100644 --- a/lib/algorithmable/data_structs/ordered_symbol_table.rb +++ b/lib/algorithmable/data_structs/ordered_symbol_table.rb @@ -2,12 +2,14 @@ module Algorithmable module DataStructs class OrderedSymbolTable extend Forwardable + include Algorithmable::Trees + + private_methods :new_binary_search_tree def_delegators :@imp, :[]=, :[], :key?, :empty?, :size, :keys, :max, :min, :floor, :ceiling, :rank, :delete def initialize(key_type, value_type) - search_strategy_factory = Object.new.extend Algorithmable::Searches - @imp = search_strategy_factory.new_binary_search_tree(key_type, value_type) + @imp = new_binary_search_tree(key_type, value_type) end end end diff --git a/lib/algorithmable/graphs.rb b/lib/algorithmable/graphs.rb index 89ac7e5..8d64230 100644 --- a/lib/algorithmable/graphs.rb +++ b/lib/algorithmable/graphs.rb @@ -1,7 +1,9 @@ module Algorithmable module Graphs - autoload :Undirected, 'algorithmable/graphs/undirected' - autoload :Traversals, 'algorithmable/graphs/traversals' + require 'algorithmable/graphs/directed' + require 'algorithmable/graphs/edge' + require 'algorithmable/graphs/traversals' + require 'algorithmable/graphs/undirected' class << self def new_undirected_graph(vertices = []) @@ -11,6 +13,14 @@ def new_undirected_graph(vertices = []) end graph end + + def new_directed_graph(vertices = []) + Directed.new.tap do |g| + vertices.each do |from, to, weight| + g.add_edge Edge.new(from, to, weight) + end + end + end end end end diff --git a/lib/algorithmable/graphs/directed.rb b/lib/algorithmable/graphs/directed.rb new file mode 100644 index 0000000..b87375c --- /dev/null +++ b/lib/algorithmable/graphs/directed.rb @@ -0,0 +1,45 @@ +module Algorithmable + module Graphs + class Directed + def initialize + @edges = 0 + @incoming_degree = [] + @adjacency_list = [] + end + + def add_edge(edge) + from = edge.from + to = edge.to + validate_vertex(to) + validate_vertex(from) + (@adjacency_list[from] ||= []) + .tap { |xs| xs << edge } + (@incoming_degree[to] ||= 0) + .tap { @incoming_degree[to] += 1 } + @edges += 1 + end + + def adjacency_list(vertex) + @adjacency_list[vertex] || [] + end + + def out_degree(vertex) + @adjacency_list[vertex].length + end + + def in_degree(vertex) + @incoming_degree[vertex] + end + + def edges + @adjacency_list.dup + end + + private + + def validate_vertex(value) + raise "invalid vertex value: #{value}" if 0 > value + end + end + end +end diff --git a/lib/algorithmable/graphs/edge.rb b/lib/algorithmable/graphs/edge.rb new file mode 100644 index 0000000..688dc4d --- /dev/null +++ b/lib/algorithmable/graphs/edge.rb @@ -0,0 +1,14 @@ +module Algorithmable + module Graphs + class Edge + attr_reader :from, :to, :weight + + def initialize(from, to, weight) + @from = from + @to = to + @weight = weight + freeze + end + end + end +end diff --git a/lib/algorithmable/graphs/traversals.rb b/lib/algorithmable/graphs/traversals.rb index 8e31729..8401fad 100644 --- a/lib/algorithmable/graphs/traversals.rb +++ b/lib/algorithmable/graphs/traversals.rb @@ -1,9 +1,10 @@ module Algorithmable module Graphs module Traversals - autoload :Errors, 'algorithmable/graphs/traversals/errors' - autoload :DepthFirst, 'algorithmable/graphs/traversals/depth_first' - autoload :BreadthFirst, 'algorithmable/graphs/traversals/breadth_first' + require 'algorithmable/graphs/traversals/errors' + require 'algorithmable/graphs/traversals/depth_first' + require 'algorithmable/graphs/traversals/breadth_first' + require 'algorithmable/graphs/traversals/topological_sort' def traverse_with_depth_first(graph, source) DepthFirst.new(graph, source) diff --git a/lib/algorithmable/graphs/traversals/breadth_first.rb b/lib/algorithmable/graphs/traversals/breadth_first.rb index 790188b..858704a 100644 --- a/lib/algorithmable/graphs/traversals/breadth_first.rb +++ b/lib/algorithmable/graphs/traversals/breadth_first.rb @@ -5,12 +5,12 @@ module Traversals class BreadthFirst include Algorithmable::Graphs::Traversals::Errors - def initialize(graph, source) + def initialize(graph, source, &f) vertices_size = graph.vertices.size @visited = Array.new(vertices_size - 1) @edge_to = Array.new(vertices_size - 1) @source = source - traverse graph, source + traverse graph, source, &f end def visited?(vertex) @@ -22,10 +22,10 @@ def path_to(vertex) path = [] finish = vertex while finish != @source - path.push finish + path.unshift finish finish = @edge_to[finish] end - path.push @source + path.unshift @source path end @@ -35,13 +35,15 @@ def traverse(graph, vertex) queue = ::Queue.new @visited[vertex] = true queue.enq vertex + yield vertex if block_given? until queue.empty? next_vertex = queue.deq - graph.adjacency(next_vertex).each do |neighbour_vertex| + graph.adjacency_list(next_vertex).each do |neighbour_vertex| next if visited? neighbour_vertex @edge_to[neighbour_vertex] = next_vertex @visited[neighbour_vertex] = true queue.enq neighbour_vertex + yield neighbour_vertex if block_given? end end end diff --git a/lib/algorithmable/graphs/traversals/depth_first.rb b/lib/algorithmable/graphs/traversals/depth_first.rb index 438888b..f67738e 100644 --- a/lib/algorithmable/graphs/traversals/depth_first.rb +++ b/lib/algorithmable/graphs/traversals/depth_first.rb @@ -4,11 +4,11 @@ module Traversals class DepthFirst include Algorithmable::Graphs::Traversals::Errors - def initialize(graph, source) + def initialize(graph, source, &f) @visited = [] @edge_to = [] @source = source - traverse graph, source + traverse graph, source, &f end def visited?(vertex) @@ -20,21 +20,22 @@ def path_to(vertex) path = [] finish = vertex while finish != @source - path.push finish + path.unshift finish finish = @edge_to[finish] end - path.push @source + path.unshift @source path end private - def traverse(graph, vertex) + def traverse(graph, vertex, &f) @visited[vertex] = true - graph.adjacency(vertex).each do |neighbour_vertex| + yield vertex if block_given? + graph.adjacency_list(vertex).each do |neighbour_vertex| unless visited? neighbour_vertex @edge_to[neighbour_vertex] = vertex - traverse graph, neighbour_vertex + traverse graph, neighbour_vertex, &f end end end diff --git a/lib/algorithmable/graphs/traversals/topological_sort.rb b/lib/algorithmable/graphs/traversals/topological_sort.rb new file mode 100644 index 0000000..8021989 --- /dev/null +++ b/lib/algorithmable/graphs/traversals/topological_sort.rb @@ -0,0 +1,31 @@ +module Algorithmable + module Graphs + module Traversals + class TopologicalSort + include Algorithmable::Graphs::Traversals::Errors + + def self.sort(graph, &f) + stack = [] + visited = [] + graph.edges.each_index do |i| + next if visited[i] + do_sort(graph, i, stack, visited, &f) + end + stack + end + + def self.do_sort(graph, vertex, stack, visited, &f) + visited[vertex] = true + graph.adjacency_list(vertex).each do |edge| + next if visited[edge.to] + do_sort graph, edge.to, stack, visited, &f + end + yield vertex if block_given? + stack.unshift vertex + end + + private_class_method :do_sort + end + end + end +end diff --git a/lib/algorithmable/graphs/undirected.rb b/lib/algorithmable/graphs/undirected.rb index 853039e..e1d96ee 100644 --- a/lib/algorithmable/graphs/undirected.rb +++ b/lib/algorithmable/graphs/undirected.rb @@ -6,20 +6,20 @@ class Undirected def initialize(vertices = 0) @vertices = vertices @edges = 0 - @adj = [] - @vertices.times { |i| @adj[i] = [] } + @adjacency_list = [] + @vertices.times { |i| @adjacency_list[i] = [] } end def add_edge(left_vertex, right_vertex) - @adj[left_vertex] ||= [] - @adj[right_vertex] ||= [] - @adj[left_vertex].push right_vertex - @adj[right_vertex].push left_vertex + @adjacency_list[left_vertex] ||= [] + @adjacency_list[right_vertex] ||= [] + @adjacency_list[left_vertex].push right_vertex + @adjacency_list[right_vertex].push left_vertex @edges = @edges.next end - def adjacency(vertex) - @adj[vertex] + def adjacency_list(vertex) + @adjacency_list[vertex] end def valid_vertex?(vertex) @@ -28,14 +28,14 @@ def valid_vertex?(vertex) def degree(vertex) fail "Vertex #{vertex} is not valid." unless valid_vertex?(vertex) - adjacency(vertex).size + adjacency_list(vertex).size end def to_s data = '' @vertices.times do |vertex| data += "( #{vertex} => " - @adj[vertex].each do |neighbor| + @adjacency_list[vertex].each do |neighbor| data += "#{neighbor} " end data += ') ' diff --git a/lib/algorithmable/searches.rb b/lib/algorithmable/searches.rb index 45eda3c..df014f2 100644 --- a/lib/algorithmable/searches.rb +++ b/lib/algorithmable/searches.rb @@ -1,14 +1,9 @@ module Algorithmable module Searches autoload :BinarySearch, 'algorithmable/search/binary_search' - autoload :BinarySearchTree, 'algorithmable/search/binary_search_tree' def binary_search(element, collection) BinarySearch.lookup(element, collection) end - - def new_binary_search_tree(key_type, value_type) - BinarySearchTree.new key_type, value_type - end end end diff --git a/lib/algorithmable/sort/bubble.rb b/lib/algorithmable/sort/bubble.rb index fc0b944..0424bc4 100644 --- a/lib/algorithmable/sort/bubble.rb +++ b/lib/algorithmable/sort/bubble.rb @@ -6,12 +6,14 @@ class Bubble # Time О(N^2), stable and slow # Space О(N) # + # Optimized implementation of Bubble sort + # def self.sort(collection) length = collection.length - 1 loop do swapped = false 0.upto(length).each do |i| - if 1 == (collection[i] <=> collection[i + 1]) + if 1 == (collection[i] <=> collection[i + 1]) #nil safe comparison swap(collection, i) swapped = true end diff --git a/lib/algorithmable/trees.rb b/lib/algorithmable/trees.rb new file mode 100644 index 0000000..afc371b --- /dev/null +++ b/lib/algorithmable/trees.rb @@ -0,0 +1,9 @@ +module Algorithmable + module Trees + autoload :BinarySearchTree, 'algorithmable/trees/bst' + + def new_binary_search_tree(k_type, v_type) + BinarySearchTree.new(k_type, v_type) + end + end +end diff --git a/lib/algorithmable/search/binary_search_tree.rb b/lib/algorithmable/trees/bst.rb similarity index 99% rename from lib/algorithmable/search/binary_search_tree.rb rename to lib/algorithmable/trees/bst.rb index 008da9b..faaf9a1 100644 --- a/lib/algorithmable/search/binary_search_tree.rb +++ b/lib/algorithmable/trees/bst.rb @@ -1,5 +1,5 @@ module Algorithmable - module Searches + module Trees class BinarySearchTree def initialize(key_type, value_type) @key_type = key_type diff --git a/lib/algorithmable/version.rb b/lib/algorithmable/version.rb index 796af69..376ce7a 100644 --- a/lib/algorithmable/version.rb +++ b/lib/algorithmable/version.rb @@ -1,3 +1,3 @@ module Algorithmable - VERSION = '0.15.0' + VERSION = '0.16.0.pre1' end diff --git a/spec/lib/data_structs/tree_spec.rb b/spec/lib/data_structs/tree_spec.rb index 2664529..a901731 100644 --- a/spec/lib/data_structs/tree_spec.rb +++ b/spec/lib/data_structs/tree_spec.rb @@ -2,7 +2,7 @@ describe Algorithmable::DataStructs::Tree do include Algorithmable::DataStructs::Tree - let(:tree) { new_ordered_binary_tree } + let(:trees) { new_ordered_binary_tree } context '#when sorting a binary tree' do it 'can sort collection in O(n) operations' do diff --git a/spec/lib/graphs/traversals/breadth_first_spec.rb b/spec/lib/graphs/traversals/breadth_first_spec.rb index 4858f2e..57a8ff6 100644 --- a/spec/lib/graphs/traversals/breadth_first_spec.rb +++ b/spec/lib/graphs/traversals/breadth_first_spec.rb @@ -10,27 +10,38 @@ let(:traversal) { described_class.new graph, 0 } it do - expect(traversal.path_to(1)).to eq([1, 0]) + expect(traversal.path_to(1)).to eq([0, 1]) end it do - expect(traversal.path_to(2)).to eq([2, 0]) + expect(traversal.path_to(2)).to eq([0, 2]) end it do - expect(traversal.path_to(3)).to eq([3, 5, 0]) + expect(traversal.path_to(3)).to eq([0, 5, 3]) end it do - expect(traversal.path_to(4)).to eq([4, 5, 0]) + expect(traversal.path_to(4)).to eq([0, 5, 4]) end it do - expect(traversal.path_to(5)).to eq([5, 0]) + expect(traversal.path_to(5)).to eq([0, 5]) end it do - expect(traversal.path_to(6)).to eq([6, 0]) + expect(traversal.path_to(6)).to eq([0, 6]) + end + end + + context 'additional BFS traversal test' do + let(:graph) do + Algorithmable::Graphs.new_undirected_graph([[0, 1], [0, 2], [1, 2], [2, 0], [2, 3], [3, 3]]) + end + let(:traversal) { described_class.new(graph, 0) } + + it 'can find path shortest path from 0 to ' do + expect(traversal.path_to(3)).to eq([0, 2, 3]) end end end diff --git a/spec/lib/graphs/traversals/depth_first_spec.rb b/spec/lib/graphs/traversals/depth_first_spec.rb index def83ef..bd322a1 100644 --- a/spec/lib/graphs/traversals/depth_first_spec.rb +++ b/spec/lib/graphs/traversals/depth_first_spec.rb @@ -10,27 +10,40 @@ let(:traversal) { described_class.new graph, 0 } it do - expect(traversal.path_to(1)).to eq([1, 0]) + expect(traversal.path_to(1)).to eq([0, 1]) end it do - expect(traversal.path_to(2)).to eq([2, 0]) + expect(traversal.path_to(2)).to eq([0, 2]) end it do - expect(traversal.path_to(3)).to eq([3, 4, 5, 0]) + expect(traversal.path_to(3)).to eq([0, 5, 4, 3]) end it do - expect(traversal.path_to(4)).to eq([4, 5, 0]) + expect(traversal.path_to(4)).to eq([0, 5, 4]) end it do - expect(traversal.path_to(5)).to eq([5, 0]) + expect(traversal.path_to(5)).to eq([0, 5]) end it do - expect(traversal.path_to(6)).to eq([6, 4, 5, 0]) + expect(traversal.path_to(6)).to eq([0, 5, 4, 6]) + end + end + + context 'additional DFS traversal test' do + let(:graph) do + Algorithmable::Graphs.new_undirected_graph([[0, 1], [0, 2], [1, 2], [2, 0], [2, 3], [3, 3]]) + end + let(:traversal) { + described_class.new(graph, 0) + } + + it 'can find path shortest path from 0 to ' do + expect(traversal.path_to(3)).to eq([0, 1, 2, 3]) end end end diff --git a/spec/lib/graphs/traversals/topological_sort_spec.rb b/spec/lib/graphs/traversals/topological_sort_spec.rb new file mode 100644 index 0000000..6d3bf30 --- /dev/null +++ b/spec/lib/graphs/traversals/topological_sort_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe Algorithmable::Graphs::Traversals::TopologicalSort do + context 'additional DFS traversal test' do + let(:graph) { + Algorithmable::Graphs.new_directed_graph([[5, 2], [5, 0], [4, 0], [4, 1], [2, 3], [3, 1]]) + } + it 'can sort DiGraph using topological sort algo' do + sorted = described_class.sort(graph) + expect(sorted).to eq([5, 4, 2, 3, 1, 0]) + end + end +end diff --git a/spec/lib/graphs/undirected_spec.rb b/spec/lib/graphs/undirected_spec.rb index c754873..fe92ae5 100644 --- a/spec/lib/graphs/undirected_spec.rb +++ b/spec/lib/graphs/undirected_spec.rb @@ -32,7 +32,7 @@ it do graph.add_edge(1, 0) graph.add_edge(1, 3) - expect(graph.adjacency(1)).to eq([0, 3]) + expect(graph.adjacency_list(1)).to eq([0, 3]) end end end diff --git a/spec/lib/searches_spec.rb b/spec/lib/searches_spec.rb index d516b6d..145f67b 100644 --- a/spec/lib/searches_spec.rb +++ b/spec/lib/searches_spec.rb @@ -10,10 +10,5 @@ search = factory.binary_search(1, [2, 4, 5, 1].sort) expect(search).to eq(0) end - - it do - tree = factory.new_binary_search_tree(String, Fixnum) - expect(tree).to be_kind_of described_class::BinarySearchTree - end end end diff --git a/spec/lib/sort/binary_heap_spec.rb b/spec/lib/sort/binary_heap_spec.rb index 2faf053..7c8b61a 100644 --- a/spec/lib/sort/binary_heap_spec.rb +++ b/spec/lib/sort/binary_heap_spec.rb @@ -6,17 +6,17 @@ let(:sorted_collection) { fixture[:sorted] } context 'when sorting' do - it 'can return ordered collection' do + it 'can return ordered collection using binary_heap' do sorted = described_class.binary_heap(collection) expect(sorted).to eq(sorted_collection) end - it 'should return ordered collection with negative values' do + it 'should return ordered collection with negative values using binary_heap' do array = [-1, 0, -2, 10] expect(described_class.binary_heap array).to eq(array.sort) end - it 'should return empty collection' do + it 'should return empty collection using binary_heap' do array = [] expect(described_class.binary_heap array).to eq(array) end diff --git a/spec/lib/sort/bubble_spec.rb b/spec/lib/sort/bubble_spec.rb index 65d256d..4235a32 100644 --- a/spec/lib/sort/bubble_spec.rb +++ b/spec/lib/sort/bubble_spec.rb @@ -5,11 +5,11 @@ let(:collection) { fixture[:input] } let(:sorted_collection) { fixture[:sorted] } - it 'can sort unsorted array' do + it 'can sort unsorted array using bubble sort' do expect(described_class.bubble(collection)).to eq(sorted_collection) end - it 'can return array with single element' do + it 'can return array with single element using bubble sort' do input = [8] expect(described_class.bubble(input)).to eq(input) end diff --git a/spec/lib/sort/insertion_spec.rb b/spec/lib/sort/insertion_spec.rb index 270a0cc..db7ecec 100644 --- a/spec/lib/sort/insertion_spec.rb +++ b/spec/lib/sort/insertion_spec.rb @@ -5,11 +5,11 @@ let(:collection) { fixture[:input] } let(:sorted_collection) { fixture[:sorted] } - it 'can sort unsorted array' do + it 'can sort unsorted array using insertion sort' do expect(described_class.insertion(collection)).to eq(sorted_collection) end - it 'can return array with single element' do + it 'can return array with single element using insertion sort' do input = [8] expect(described_class.insertion(input)).to eq(input) end diff --git a/spec/lib/sort/merge_spec.rb b/spec/lib/sort/merge_spec.rb index bfccc8c..4a8f949 100644 --- a/spec/lib/sort/merge_spec.rb +++ b/spec/lib/sort/merge_spec.rb @@ -5,11 +5,11 @@ let(:collection) { fixture[:input] } let(:sorted_collection) { fixture[:sorted] } - it 'can sort unsorted array' do + it 'can sort unsorted array using merge sort' do expect(described_class.merge(collection)).to eq(sorted_collection) end - it 'can return array with single element' do + it 'can return array with single element using merge sort' do input = [8] expect(described_class.merge(input)).to eq(input) end diff --git a/spec/lib/sort/quick_sort_spec.rb b/spec/lib/sort/quick_sort_spec.rb index 295c558..215a1a9 100644 --- a/spec/lib/sort/quick_sort_spec.rb +++ b/spec/lib/sort/quick_sort_spec.rb @@ -5,16 +5,16 @@ let(:collection) { fixture[:input] } let(:sorted_collection) { fixture[:sorted] } - it 'can sort unsorted array' do + it 'can sort unsorted array using quick sort' do expect(described_class.quick(collection)).to eq(sorted_collection) end - it 'can sort unsorted large array' do + it 'can sort 1000*1000 unsorted array using quick sort' do input = 1000.times.map { collection.shuffle }.flatten expect(described_class.quick(input).uniq).to eq(sorted_collection) end - it 'can return array with single element' do + it 'can return array with single element using quick sort' do input = [8] expect(described_class.quick(input)).to eq(input) end diff --git a/spec/lib/sort/selection_sort_spec.rb b/spec/lib/sort/selection_sort_spec.rb index 82fb954..f7ebdeb 100644 --- a/spec/lib/sort/selection_sort_spec.rb +++ b/spec/lib/sort/selection_sort_spec.rb @@ -5,11 +5,11 @@ let(:collection) { fixture[:input] } let(:sorted_collection) { fixture[:sorted] } - it 'can sort unsorted array' do + it 'can sort unsorted array using selection sort' do expect(described_class.selection(collection)).to eq(sorted_collection) end - it 'can return array with single element' do + it 'can return array with single element using selection sort' do input = [8] expect(described_class.selection(input)).to eq(input) end diff --git a/spec/lib/sort/shell_spec.rb b/spec/lib/sort/shell_spec.rb index 4b8694b..d78898f 100644 --- a/spec/lib/sort/shell_spec.rb +++ b/spec/lib/sort/shell_spec.rb @@ -5,11 +5,11 @@ let(:collection) { fixture[:input] } let(:sorted_collection) { fixture[:sorted] } - it 'can sort unsorted array' do + it 'can sort unsorted array using shell sort' do expect(described_class.shell(collection)).to eq(sorted_collection) end - it 'can return array with single element' do + it 'can return array with single element using shell sort' do input = [8] expect(described_class.shell(input)).to eq(input) end diff --git a/spec/lib/sort/shuffle_spec.rb b/spec/lib/sort/shuffle_spec.rb index 5d68951..3b024f0 100644 --- a/spec/lib/sort/shuffle_spec.rb +++ b/spec/lib/sort/shuffle_spec.rb @@ -5,11 +5,11 @@ let(:collection) { fixture[:input] } let(:sorted_collection) { fixture[:sorted] } - it 'can sort unsorted array' do + it 'can sort shuffled array using shuffle algo' do expect(described_class.shuffle(sorted_collection)).to contain_exactly(*collection) end - it 'can return array with single element' do + it 'can return array with single element using shuffle algo' do input = [8] expect(described_class.shell(input)).to eq(input) end diff --git a/spec/lib/search/binary_search_tree_spec.rb b/spec/lib/trees/binary_search_tree_spec.rb similarity index 96% rename from spec/lib/search/binary_search_tree_spec.rb rename to spec/lib/trees/binary_search_tree_spec.rb index dc9278c..b99c8c8 100644 --- a/spec/lib/search/binary_search_tree_spec.rb +++ b/spec/lib/trees/binary_search_tree_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Algorithmable::Searches::BinarySearchTree do +describe Algorithmable::Trees::BinarySearchTree do let(:input) { %w(n y e c a q p b k t s v w f z j g r u m o i h l x).freeze } let(:tree) do new_tree = described_class.new(String, Numeric)