Skip to content

Instantly share code, notes, and snippets.

View pdkproitf's full-sized avatar

Ryan Pham pdkproitf

View GitHub Profile
@pdkproitf
pdkproitf / max_heap_sort.rb
Created July 13, 2019 11:55
Implement heap sort in ruby
# Algorithms: https://www.geeksforgeeks.org/heap-sort/
module MaxHeapSort
extend self
@heap = Array.new
@sorted = Array.new
def create_heap(arr)
arr.each_with_index { |num, index| insert(index, num) }
@pdkproitf
pdkproitf / min_heap_sort.rb
Last active July 14, 2019 06:40
Implement Heap Sort in ruby
module MinHeapSortV1
module_function
def heap_sort(array)
n = array.length
a = [nil] + array
(n / 2).downto(1) { |i| heapify(a, i, n) }
while n > 1
@pdkproitf
pdkproitf / quick_sort.rb
Created July 13, 2019 11:52
Implement quick sort in ruby
# Algorithms: https://www.geeksforgeeks.org/quick-sort/
def quick_sort(array)
return array if array.length <= 1
pivot = array.pop
l = -1
for i in 0...array.length
next unless array[i] < pivot
@pdkproitf
pdkproitf / merge_sort.rb
Last active July 13, 2019 11:51
Implement merge sort in ruby
# Algorithms: https://www.geeksforgeeks.org/merge-sort/
def merge_sort(array)
return array if array.length <= 1
mid = (array.length / 2)
left = merge_sort(array[0..mid - 1])
right = merge_sort(array[mid..-1])
merge(left, right)
end
@pdkproitf
pdkproitf / selection_sort.rb
Last active July 13, 2019 11:48
Implement Selection Sort in ruby
# Algorithms: https://www.geeksforgeeks.org/selection-sort/
def selection_sort(array)
(array.length - 1).times do |i|
min_index = i
for j in (i + 1)...array.length
min_index = j if array[min_index] > array[j]
end
array[min_index], array[i] = array[i], array[min_index] if min_index != i
@pdkproitf
pdkproitf / insertion_sort.rb
Last active July 13, 2019 11:49
Implement Insertion Sort in Ruby
# Algorithms: https://www.geeksforgeeks.org/insertion-sort/
def insertion_sort(array)
(array.length).times do |j|
while j > 0
break if array[j - 1] < array[j]
array[j], array[j - 1] = array[j - 1], array[j]
j -= 1
end
@pdkproitf
pdkproitf / bubble_sort.rb
Last active July 13, 2019 11:48
Bubble Sort in Ruby
# Algorithm: https://www.geeksforgeeks.org/bubble-sort/
def bubble_sort_recursive(array)
limit_sort = array.length - 1
return array if limit_sort <= 1
print "#{limit_sort} ----> #{array}\n"
for i in 0...limit_sort
next unless array[i] > array[i + 1]
@pdkproitf
pdkproitf / run_rails_production_mode.markdown
Last active December 20, 2021 11:51
Run rails production mode

RAILS <= 5.2

  1. Set secret key: To create a secret key, run the following command: bundle exec rake secret Copy the secret key and put it to config/secrets.yml file with the following format:
production:
 secret_key_base: <%=ENV["SECRET_KEY_BASE"]%>
  1. Create credentials
  • Remove the old credentials:rm config/credentials.yml.enc