Commit 61b66fa9 authored by Owen Ilagan's avatar Owen Ilagan

New Code

parent b6398460
def sort_ascending(array)
# Clone the original array
result = array[0..-1]
# Do Bubble Sorting
index = 0
result.each do
sub_array = result[index+1..-1]
sub_index = 0
sub_array.each do
if sub_array[sub_index] < result[index]
temp = result[index]
result[index] = sub_array[sub_index]
result[index+1+sub_index] = temp
end
sub_index += 1
end
index += 1
end
return result
end
def sort_descending(array)
# Clone the original array
result = array[0..-1]
# Do Bubble Sorting
index = 0
result.each do
sub_array = result[index+1..-1]
sub_index = 0
sub_array.each do
if sub_array[sub_index] > result[index]
temp = result[index]
result[index] = sub_array[sub_index]
result[index+1+sub_index] = temp
end
sub_index += 1
end
index += 1
end
return result
end
my_array = [512 , 128 , 256, 1024, 16, 64, 32, 4]
puts "Original Array: " + my_array.join(',')
puts "Ascending: " + sort_ascending(my_array).join(',')
puts "Original Array: " + my_array.join(',')
puts "Descending: " + sort_descending(my_array).join(',')
# Fibonacci Function from
# http://www.dotnetperls.com/fibonacci-ruby
def fibonacci(n)
a = 0
b = 1
# Compute Fibonacci number in the desired position.
n.times do
temp = a
a = b
# Add up previous two numbers in sequence.
b = temp + b
end
return a
end
# Write first 15 Fibonacci numbers in sequence.
15.times do |n|
result = fibonacci(n)
puts result
end
n = 15
def get_fibonacci(length)
array = []
# Write first 15 Fibonacci numbers in sequence.
length.times do |n|
result = fibonacci(n)
array.push(result)
end
return array
end
puts 'When N=' + n.to_s + ' Fibonacci is ' + get_fibonacci(n).join(',')
...@@ -18,7 +18,7 @@ def get_reverse(array) ...@@ -18,7 +18,7 @@ def get_reverse(array)
return result return result
end end
puts numbers
puts 'Numbers are: ' + numbers.join(',') puts 'Numbers are: ' + numbers.join(',')
puts 'Average value is ' + get_average(numbers).to_s puts 'Average value is ' + get_average(numbers).to_s
puts 'Reverse is ' + get_reverse(numbers).join(',') puts 'Reverse is ' + get_reverse(numbers).join(',')
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment