Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sort array (Bubble sort) Exercise
# # Sort the array from lowest to highest
def sort_Anderson(arr)
# arr.sort #initial code
flag =true
while flag
arr_before = arr.clone
arr = check_next(arr)
flag = false if arr_before == arr
end
return arr
end
def check_next (arr) #buble sort algorithm
arr.each_index do |i|
unless i+1 == arr.length
temp = arr[i]
arr[i] = arr[i+1] if arr[i] > arr[i+1]
arr[i+1] = temp if arr[i] == arr[i+1]
end
end
return arr
end
# Find the maximum
def maximum(arr)
sort_Anderson(arr).last
end
def minimum(arr)
sort_Anderson(arr).first
end
# expect it to return 42 below
result = maximum([2, 42, 22, 02])
puts "max of 2, 42, 22, 02 is: #{result}"
# expect it to return 2 below
result = minimum([2, 42, 22, 02])
puts "min of 2, 42, 22, 02 is: #{result}"
# expect it to return nil when empty array is passed in
result = maximum([])
puts "max on empty set is: #{result.inspect}"
result = minimum([])
puts "min on empty set is: #{result.inspect}"
result = maximum([-23, 0, -3])
puts "max of -23, 0, -3 is: #{result}"
result = maximum([6])
puts "max of just 6 is: #{result}"
puts "Testing sort algorithm with big array"
print "#{sort_Anderson([2312,512,63,23,3,2,6,12,53,56,865,3,2,23,1,5,8,94,2,34,6,53,234,8,34,654,2,])} \n"
puts "\n Benchmark Test\n"
require 'benchmark' #Add benchmark library
array = (1..1000).map { rand(1000) }
Benchmark.bmbm do |x|
x.report("sort_Anderson") { sort_Anderson(array) }
x.report("sort_bult-in") { array.sort }
end
# # Sort the array from lowest to highest
def sort_Anderson(arr)
# arr.sort #initial code
while true
arr_before = arr.clone
arr = check_next(arr)
break if arr_before == arr
end
return arr
end
def check_next (arr) #buble sort algorithm
arr.each_index do |i|
( arr[i], arr[i+1] = arr[i+1], arr[i] ) if ( arr[i] > arr[i+1] ) unless ( i+1 == arr.length )
end
return arr
end
# Find the maximum
def maximum(arr)
sort_Anderson(arr).last
end
def minimum(arr)
sort_Anderson(arr).first
end
# expect it to return 42 below
result = maximum([2, 42, 22, 02])
puts "max of 2, 42, 22, 02 is: #{result}"
# expect it to return 2 below
result = minimum([2, 42, 22, 02])
puts "min of 2, 42, 22, 02 is: #{result}"
# expect it to return nil when empty array is passed in
result = maximum([])
puts "max on empty set is: #{result.inspect}"
result = minimum([])
puts "min on empty set is: #{result.inspect}"
result = maximum([-23, 0, -3])
puts "max of -23, 0, -3 is: #{result}"
result = maximum([6])
puts "max of just 6 is: #{result}"
puts "Testing sort algorithm with big array"
print "#{sort_Anderson([2312,512,63,23,3,2,6,12,53,56,865,3,2,23,1,5,8,94,2,34,6,53,234,8,34,654,2,])} \n"
puts "\n Benchmark Test\n"
require 'benchmark' #Add benchmark library
array = (1..1000).map { rand(1000) }
Benchmark.bmbm do |x|
x.report("sort_Anderson") { sort_Anderson(array) }
x.report("sort_bult-in") { array.sort }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment