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:
I hereby claim:
To claim this, I am signing this object:
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 |
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 |
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 |
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) |
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 |
class DFS | |
def initialize(graph, source_node) | |
@graph = graph | |
@source_node = source_node | |
@visited = [] | |
@edge_to = {} | |
dfs(source_node) | |
end |
require "set" | |
class Node | |
attr_accessor :name, :adjacents | |
def initialize(name) | |
@adjacents = Set.new | |
@name = name | |
end |
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 |
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 | |