Skip to content

Instantly share code, notes, and snippets.

View anuonifade's full-sized avatar
💭
Don't stop learning

Anu Onifade anuonifade

💭
Don't stop learning
View GitHub Profile

Keybase proof

I hereby claim:

  • I am anuonifade on github.
  • I am anuonifade (https://keybase.io/anuonifade) on keybase.
  • I have a public key ASBhVFr2GMxq9cWEkxanOdBdJPW-H-qRGTDNwZ5RhzE1FQo

To claim this, I am signing this object:

@anuonifade
anuonifade / insertion_sort.rb
Last active February 13, 2019 16:58
Ruby implementation of Insertion sort
def insertion_sort(array)
return array if array.size <= 1 # return if array length is either 0 or 1 as the array in itself is sorted
(array.size).times do |j|
while j > 0
if array[j - 1] > array[j]
array[j - 1], array[j] = array[j], array[j - 1]
else
break
end
@anuonifade
anuonifade / merge_sort.rb
Last active February 13, 2019 10:40
Ruby implementation of Merge sort
def merge_sort(array)
if array.size <= 1 # If array size is less or equal to 1, the array is sorted in himself
array # so we return the array
else
mid = (array.size / 2).floor # Divide the array into to two parts, left and right
left = merge_sort(array[1..mid-1]) # Perform a merge_sort on the left array
right = merge_sort(array[mid..array.size]) # Perform a merge_sort on the right array
merge(left, right) # Merge the left and right array
end
end
@anuonifade
anuonifade / quick_sort.rb
Created February 12, 2019 22:44
Ruby implementation of Quick sort
def quick_sort(array)
return array if array.size <= 1
pivot = array.delete_at(rand(array.size))
# Create the left and right subarrays
left = Array.new
right = Array.new
array.each do |x|
if x <= pivot
@anuonifade
anuonifade / heapsort.rb
Last active February 12, 2019 20:19
Ruby implementation of heapsort
class HeapSort
def heap_sort(array)
n = array.size - 1 # Gets the last index of the array
a = array # Stores array temporarily in a
# Create a max heap from the existing array
# Such that the max element is placed at the root of the tree
(n/2).downto(0) do |i|
create_max_heap(a, i, n)
@anuonifade
anuonifade / selection_sort.rb
Created February 11, 2019 22:37
Selection sort implementation in Ruby
def selection_sort(array)
n = array.size - 1
n.times do |i|
(i + 1).upto(n) { |j| array[i], array[j] = array[j], array[i] if array[i] > array[j] }
end
array
end
@anuonifade
anuonifade / DFS.rb
Created February 11, 2019 17:34
Implementation of Depth First Search Algorithm in Ruby
class DFS
def initialize(graph, source_node)
@graph = graph
@source_node = source_node
@visited = []
@edge_to = {}
dfs(source_node)
end
@anuonifade
anuonifade / node.rb
Created February 11, 2019 16:47
Creating node of a graph
require "set"
class Node
attr_accessor :name, :adjacents
def initialize(name)
@adjacents = Set.new
@name = name
end
@anuonifade
anuonifade / graph.rb
Created February 11, 2019 16:45
Graph with nodes and edges
class Graph
# We are dealing with an undirected graph,
# so I increment the "adjacents" in both sides.
# The depth first will work the same way with
# a directed graph.
def add_edge(node_a, node_b)
node_a.adjacents << node_b
node_b.adjacents << node_a
end
@anuonifade
anuonifade / astar.rb
Last active February 13, 2019 14:24
A-star algorithm implementation in Ruby
class ASTAR
def initialize(start, stop)
[start, stop].each do |e|
raise ArgumentError,
"Required a Node as input." +
"Received a #{e.class}" unless e.is_a? Node
end