This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# check balanced parentheses in ruby : | |
# | |
# $ ruby check_balanced_parentheses.rb | |
# Enter string : | |
# ((())) | |
# true | |
def check_balanced_parentheses?(entered_string) | |
stack = [] | |
entered_string.chars.each do |token| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given an array of integers, find the 3 numbers that sum up to a target number. | |
# ruby find_3_Numbers_that_sum_to_target_number.rb | |
# Enter the size of array : 5 | |
# Enter 1 element : | |
# 1 | |
# Enter 2 element : | |
# 2 | |
# Enter 3 element : | |
# 3 | |
# Enter 4 element : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Roots of quadratic equation | |
# | |
# $ ruby roots_of_quadratic_equations.rb | |
# Equation will be in the format : ax^2 + bx + c | |
# Enter the value of a : | |
# 2 | |
# Enter the value of b : | |
# 3 | |
# Enter the value of c : | |
# 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Swapping two variables in ruby : | |
# | |
# $ ruby swap_two_variables.rb | |
# Enter first variable : | |
# 2 | |
# Enter second variable : | |
# 3 | |
# After exchanging the value : | |
# the value of a is :3 | |
# the value of b is :2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# selection_sort.rb | |
# | |
# $ ruby selection_sort.rb | |
# [1, 2, 3, 4, 5, 6, 7, 8] | |
def selection_sort(array) | |
n = array.length-1 | |
for i in 0..n-1 | |
min = i | |
j = i + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# quick_sort.rb | |
# | |
# $ ruby quick_sort.rb | |
# [1, 2, 3, 4, 5, 6, 7, 8] | |
def quicksort(array) | |
return array if array.length <= 1 | |
pivot_index = (array.length / 2).to_i | |
pivot_value = array[pivot_index] | |
array.delete_at(pivot_index) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# merge_sort.rb | |
# | |
# $ ruby merge_sort.rb | |
# [1, 2, 3, 4, 5, 6, 7, 8] | |
def merge_sort(array) | |
n = array.length | |
if n > 1 | |
mid = n/2 | |
lefthalf = array[0..mid-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# insertion_sort.rb | |
# | |
# $ ruby insertion_sort.rb | |
# [1, 2, 3, 4, 5, 6, 7, 8] | |
def insertion_sort(array) | |
for i in 1..array.length - 1 | |
j = i | |
temp = array[i] | |
while ((j>0) and (array[j-1] > temp)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bubble_sort.rb | |
# | |
# $ ruby bubble_sort.rb | |
# [1, 2, 3, 4, 5, 6, 7, 8] | |
def bubble_sort(array) | |
for i in 0..array.length - 1 | |
for j in 0..array.length - 2 | |
if array[j] > array[j+1] | |
array[j],array[j+1] = array[j+1],array[j] |