Hi I’m having the hardest time create Bubble Sort w/o using the Proc
Here is some of the logic. I was able to get it to Bubble sort once, but that’s all. I was thinking of writing some while loop that would wrap around this logic, or am I doing this wrong?
def bubble_sort!(arg)
i = 0
while i < arg.length - 1
if (arg[i] <=> arg[i + 1]) == 1
temp = arg[i]
arg[i] = arg[i + 1]
arg[i + 1] = temp
end
i += 1
end
arg
end
Test case: bubble_sort!([1, 4, 3, 9, 0, 3])
Thank you!
That is correct. You will need a while loop to wrap all the logic you wrote. This while loop should end after your array is sorted. You will have to figure out what conditions need to be satisfied for your while loop to end.
def is_sorted(arg)
i = 0
while i < arg.length - 1
return false if arg[i] > arg[i + 1]
i += 1
end
return true
end
def bubble_sort!(arg)
i = 0
while is_sorted(arg) != true
while i < arg.length - 1
if (arg[i] <=> arg[i + 1]) == 1
temp = arg[i]
arg[i] = arg[i + 1]
arg[i + 1] = temp
end
i += 1
end
end
return arg
end
arg = [1, 3, 4, 5, 3]
bubble_sort!(arg)
Technically, I thought this would work? Did I create an infinite loop, or does the amount of imbedded loops make this take forever to run? Any suggestions? Thank you!
You have to move the i = 0 inside the first while loop. What is happening is i is incremented to arg.length - 1 from the first iteration and stays at arg.length - 1. So, your inside while loop will only run once for the array and then get stuck there. You will need to reset your i, if you want to scan the array again for possible swaps.
Oh, I see! Thank you, I fixed it!
As I tried to put it back into the actual Bubble sort, and I do not even know where to begin to put the prc.call. I’m looking at the tests and I found both proc/blocks confusing. I’m not even sure how the test case should look like. Thank you again Mi-Ruan!
class Array
def is_sorted(arg)
i = 0
while i < arg.length - 1
return false if arg[i] > arg[i + 1]
i += 1
end
return true
end
def bubble_sort!(&prc)
while is_sorted(self) != true
i = 0
while i < self.length - 1
if (self[i] <=> self[i + 1]) == 1 #prc.call here?
temp = self[i]
self[i] = self[i + 1]
self[i + 1] = temp
i += 1
end
i += 1
end
end
return self
end
end
Lastly, how should the prc.call look? Is this correct below? I’m not quite sure.
[1, 3, 5].bubble_sort! { |num1, num2| num1 <=> num2 } #sort ascending
[1, 3, 5].bubble_sort! { |num1, num2| num2 <=> num1 } #sort descending
Yep. That is where the proc should be called. The line in your edited post is correctly using the proc. Your bubble_sort! will be accepting a block as an argument exactly like how you wrote out in bold. You will still have to think about the edge case where no proc is given to you for your bubble_sort! and figure out how to solve that.
Is this something along those lines? Excuse the mess! I also managed to crash my terminal. I’m not sure where. I tried reading the solution and just got more confused.
class Array
def is_sorted(arg)
i = 0
while i < arg.length - 1
return false if arg[i] > arg[i + 1]
i += 1
end
return true
end
def bubble_sort!(&prc)
while is_sorted(self) != true
i = 0
while i < self.length - 1
if (block_given?)
if prc.call(self[i], self[i + 1]) == true
temp = self[i]
self[i] = self[i + 1]
self[i + 1] = temp
i+= 1
end
i += 1
else
if (self[i] <=> self[i + 1]) == -1
temp = self[i]
self[i] = self[i + 1]
self[i + 1] = temp
i+= 1
end
i += 1
end
end
end
return self
end
end
Blockquote
prc.call(self[i], self[i+ 1]) == true
(self[i] <=> self[i + 1]) == -1
prc.call(self[i], self[i+1]), the prc is actually a spaceship operator block and returns 0, 1, -1. Use this knowledge to figure out how you want to create the conditional.
self[i] <=> self[i+1] == -1 is true if self[i+1] > self[i], but this is probably not what you want if you want to make a swap.
Thank you Mi-Ruan,
def is_sorted(arg)
i = 0
while i < arg.length - 1
return false if arg[i] > arg[i + 1]
i += 1
end
return true
end
def bubble_sort!(&prc)
while is_sorted(self) != true
i = 0
while i < self.length - 1
if (block_given?)
if prc.call(self[i], self[i + 1]) == 1
temp = self[i]
self[i] = self[i + 1]
self[i + 1] = temp
end
i += 1
else
if (self[i] <=> self[i + 1]) == 1
temp = self[i]
self[i] = self[i + 1]
self[i + 1] = temp
end
i += 1
end
i += 1
end
end
return self
end
I believe I fixed the solution, but I can’t seem to see what is crashing my terminal. My terminal trails off and crashes. I can’t complete the error log.
It is because the the outside while loop is infinite if you never successfully sort the array hence crashing the terminal.
Oh I see, basically, when you want [5,4,3,2,1] the outside while loop would not work. I’m not sure what to do in this case? If I remove the outside while loop, the inside while loop would only run once and not continue to sort until it’s done. Thank you Mi-Ruan and I’m sorry I’m not as quick to the answer as some other posters.
Your is_sorted method will only sort in increasing order. You will have to adjust it so it can sort in reverse too, probably by using a proc. You will also probably noticed after that point, your is_sorted method and bubble_sort! look awfully similar and is_sorted is a redundant method. It would be nice to figure out a way to simplify your code by combining is_sorted and bubble_sort!.