Skip to content

Instantly share code, notes, and snippets.

View DanielAmah's full-sized avatar

Daniel Amah DanielAmah

View GitHub Profile
def bubble_sort(arr)
arr_length = arr.length
swapped = true
while swapped
swapped = false
(arr_length - 1).times do |i|
if arr[i] > arr[i + 1]
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swapped = true
end
# implementation of insertion sort
# Pick an item from an array
# compare all items in the sorted sub-list
# shift all items in the sorted sub-list greater than the item to be sorted
# insert the item
# repeat until the list is sorted.
def insertion_sort(arr)
a_length = arr.length
a_length.times do |i|
def selection_sort(arr)
n = arr.length - 1
n.times do |i|
min_index = i
((i + 1)..n).each do |j|
min_index = j if arr[j] < arr[min_index]
end
arr[i], arr[min_index] = arr[min_index], arr[i] if min_index != i
end
arr
# We would use a binary tree to implement Breadth First Search(BFS)
# We could use a shift or push to add a remove from an array or
# we use queue to help implement BFS, we use "deq" to remove an node and "enq"
# to add node.
# 1. Add root node to the queue, and mark it as visited.
# 2. Loop on the queue as long as it's not empty.
# 1. Get and remove the node at the top of the queue(current).
# 2. For every non-visited child of the current node, do the following:
# 1. Mark it as visited.
# 2. Check if it's destination node, If so, then return it.
# Implement Depth First Search using Binary Search Tree
# uses stack to perform the DFS.
# Precedure
# 1. Add root node to the stack.
# 2. Loop on the stack as long as it's not empty.
# 1. Get the node at the top of the stack(current), mark it as visited,
# 2. For every non-visited child of the current node, do the following:
# 1. Check if it's the destination node, If so, then return this child node.
# 2. Otherwise, push it to the stack.
# 3. If stack is empty, then destination node was not found!
# Heap Sorting Algorithm
# the root node of the heap is always the largest value
# build a sorted array by repeatedly removing the root node
# push to our new array
# heapify the max heap so that the new, largest value node is at the root.
def heap_sort(array)
n = array.length - 1
a = array
# First is to check if the array.length is less or equal to 1. If it is, it means the array is already sorted.
# Pick a pivot at random
# Delete the item at a specific index using ruby delete_at inbuild method
# We will using rand to get a randomized index from the array to make as our pivot.
# Save that item as the pivot
# Create a new left and right subarray
# Loop through all the items on the array and compare them with the pivot
# If an item is less than the pivot it should be added to the left subarray and if the item is more than the pivot, it should be added to the right subarray.
def quick_sort(array)
# Merge Sort Implementation
# makes a recursive call on itself until the condition is met.
# merge all sub-arrays in a sorted manner
def merge_sort(array)
# check if the array is less or equal 1, return the array.
if array.length <= 1
array
else
mid = (array.length / 2).floor # divides the array in half and recursively call the merge_sort method
## Using Bootstrap 4.0 and with the assumption that kaminari pagination gem is installed.
<div class="container">
<% @subscriptions.each do |subscription| %>
<div class="row">
<div class="col-sm">
<%= subscription.email
<div>
<div class="col-sm">
<%= subscription.plan_id
## We can leverage on eager loading using joins to load the plans with the subscription in one single query.
def index
@subscriptions = Subscription.joins(:plan).order(created_at: :desc).page(params[:page]).per(100)
end
## As a sidenote, I would move the logic for the order into a scope in the Subscription model
## Something like this
class Subscription < ApplicationRecord