Skip to content

Instantly share code, notes, and snippets.

View Nitesh-Mishra's full-sized avatar

Nitesh Mishra Nitesh-Mishra

View GitHub Profile
@Nitesh-Mishra
Nitesh-Mishra / check_balanced_parentheses.rb
Created June 9, 2018 21:42
check balanced parentheses in ruby
# 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|
@Nitesh-Mishra
Nitesh-Mishra / find_3_Numbers_that_sum_to_target_number.rb
Last active September 11, 2017 19:40
find the 3 numbers that sum to a target number.
# 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 :
@Nitesh-Mishra
Nitesh-Mishra / roots_of_quadratic_equations.rb
Created September 11, 2017 19:28
Roots of quadratic equation
# 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
@Nitesh-Mishra
Nitesh-Mishra / swap_two_variables.rb
Created September 11, 2017 19:21
Swapping two variables in ruby
#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
@Nitesh-Mishra
Nitesh-Mishra / selection_sort.rb
Created September 11, 2017 19:16
Selection Sort program in ruby
# 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
@Nitesh-Mishra
Nitesh-Mishra / quick_sort.rb
Created September 11, 2017 19:13
Quick Sort program in ruby
# 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)
@Nitesh-Mishra
Nitesh-Mishra / merge_sort.rb
Last active September 11, 2017 19:10
Merge Sort program in ruby
# 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]
@Nitesh-Mishra
Nitesh-Mishra / insertion_sort.rb
Last active September 11, 2017 19:06
Insertion Sort program in ruby
# 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))
@Nitesh-Mishra
Nitesh-Mishra / bubble_sort.rb
Last active September 11, 2017 19:01
Bubble Sort program in ruby
# 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]