algorithm_selector Gem Version

Description

Calculates algorithms and data structures for sorting and searching. Selects best one to use for particular data set.

Additionally, data structures can be used for own purposes. All data structures have the ability to insert and search. Will add deletion in the future.

Written by Joseph Bui.

Currently supports the following algorithms and data structures:

  • Sorting ("sort")
    • Bubble Sort ("bubble_sort")
    • Insertion Sort ("insertion_sort")
    • Selection Sort ("selection_sort")
    • Merge Sort ("merge_sort")
    • Quick Sort("quick_sort")
    • Heap Sort("heap_sort")
  • Searching ("search")
    • Stack ("stack")
    • Queue ("queue")
    • Singly-Linked List ("linked_list")
    • Binary Search Tree ("binary_tree")

Parameters

  • action is the string of the action, can be "sort" or "search"
  • data_set has to be in array format
  • trials is number of test trials (optional, default = 1)
  • target is element user wants to search (unnecessary for sort, default = null)
  • algorithm is the string of the algorithm or data structure

Methods

  • all(action, data_set, trials, target)
    • Returns the times for each algorithm
    • Example:
AlgorithmSelector.all("sort", [2,1,4,3], 1000)
AlgorithmSelector.all("search", [2,1,4,3], 1000, 4)
  • best(action, data_set, trials, target)
    • Returns the algorithm with the best time
    • Example:
AlgorithmSelector.best("sort", [2,1,4,3], 1000)
AlgorithmSelector.best("search", [2,1,4,3], 1000, 4)
  • analyze(action, data_set, algorithm, trials, target)
    • Returns time for specified algorithm or data structure
    • Example:
AlgorithmSelector.analyze("sort", [2,1,4,3], "merge_sort", 1000)
AlgorithmSelector.analyze("search", [2,1,4,3], "linked_list", 1000, 4)
  • compare(action, data_set, first_algorithm, second_algorithm, trials, target)
    • Returns the times of two specified algorithms for comparison
    • Example:
AlgorithmSelector.compare("sort", [2,1,4,3], "merge_sort", "quick_sort", 1000)
AlgorithmSelector.compare("search", [2,1,4,3], "stack", "linked_list", 1000, 4)
  • sort(data_set, algorithm)
    • Returns sorted array of specified sorting algorithm
    • Example:
AlgorithmSelector.sort([2,1,4,3], "bubble_sort")

Data Structures

Stack

  • Initialize
size = 10
stack = Stack.new(size)
  • Insertion
# returns nil
value = 6
stack.push(value)
  • Search
# returns true if found, else returns false
target = 4
stack.search(target)

Queue

  • Initialize
size = 10
queue = Queue.new(size)
  • Insertion
# returns nil
value = 6
queue.enqueue(value)
  • Search
# returns true if found, else returns false
target = 4
queue.search(target)

Linked List

  • Initialize
linked_list = LinkedList.new
  • Insertion
# returns nil
value = 6
linked_list.insert(value)
  • Search
# returns true if found, else returns false
target = 4
linked_list.search(target)

Binary Search Tree

  • Initialize
binary_tree = BinaryTree.new
  • Insertion
# returns nil
value = 6
binary_tree.insert(value)
  • Search
# returns true if found, else returns false
target = 4
binary_tree.search(target)

Installation

Add this line to your application's Gemfile:

gem 'algorithm_selector'

And then execute:

$ bundle

Or install it yourself as:

$ gem install algorithm_selector

Usage

Require the Gem:

$ require 'algorithm_selector'

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jnvbui/algorithm_selector. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.