Hello all, I’m a n00b that’s just getting into programming.
I’m going through Chris P.'s excellent book ‘Learn to Program’. I was
doing pretty good until I hit chapter 10 and ran into an exercise that
asks you to sort an array without using .sort.
It’s meant to use loops and not recursion (that comes later). I’ve been
spending a lot of
time on it and now I’m not quite sure how to get it working. It looks
like it’s in an infinite
loop and I’m not sure why. Can anyone point out what I need to get it
working? Right now I just want to get it working (no matter how ugly)
before I look to
improve.
File attached and pasted below
Thanks
J
word = [‘beta’, ‘zeta’, ‘alpha’, ‘depha’, ‘cina’, ‘emma’, ‘bonny’,
‘falco’]
copy array to new unsorted array
unsorted = word
initialize new sorted array
sorted = []
initialize new unsorted array used in loop
new_unsorted = []
#first loop that ends when unsort is empty
while unsorted.length > 0
#this take the last position in the unsort array pops it off aand points
smallest variable at it.
smallest = unsorted.pop
#nested loop that does actually word comparison and finds smallest word
while creating a new array. It ends when it has cycled through array
unsorted.each do |testword|
if testword < smallest
new_unsorted.push smallest
smallest = testword
else
new_unsorted.push testword
end
end
#Adds smallest word to sort array
sorted.push smallest
#unsorted array now assigned to the new_unsorted which no longer has
smallest word in it before it loops back.
unsorted = new_unsorted
end
#prints out sorted array
puts sorted
puts
#prints out inital unsorted array
puts word
puts
#prints out initial unsorted array with sort method to QA
puts word.sort