# Confused, need help with a sorting program

**URL:** <https://www.ruby-forum.com/t/confused-need-help-with-a-sorting-program/109819>\
**Category:** Ruby\
**Created:** [July 19, 2007, 10:31am UTC](https://www.ruby-forum.com/t/confused-need-help-with-a-sorting-program/109819 "2007-07-19T10:31:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johny.lam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@johny.lam](https://www.ruby-forum.com/u/johny.lam)\
**Post date:** [July 19, 2007, 10:31am UTC](https://www.ruby-forum.com/t/confused-need-help-with-a-sorting-program/109819/1 "2007-07-19T10:31:58Z")

</div>

I was doing the recursive sorting program in Chapter 10 of Chris  
Pine’s learning to program and i wrote this:

def sort some\_array  
recursive\_sort some\_array, []  
end

def recursive\_sort unsorted\_array, sorted\_array  
if unsorted\_array.length != 0  
0.upto(unsorted\_array.length-2) do |i|  
if (unsorted\_array[i+1] != nil) and (unsorted\_array[i] \<  
unsorted\_array[i+1])  
unsorted\_array[i], unsorted\_array[i+1] = unsorted\_array[i+1],  
unsorted\_array[i]  
end  
end  
sorted\_array.push(unsorted\_array.last)  
unsorted\_array.pop  
sort(unsorted\_array)  
end

if unsorted\_array.length==0  
puts sorted\_array  
end  
end

word=[]

puts ‘Input Words’  
while word.last!=’’  
word.push gets.chomp.downcase  
end

sort(word)

* * *

what i don’t get is that this program sorts the words into reverse  
alphabetical order.  
but if i switch the less than sign in “if (unsorted\_array[i+1] != nil)  
and (unsorted\_array[i] \< unsorted\_array[i+1])” to a greater than sign  
the program works correctly.

what i am trying to is move the smallest word into the sorted\_array  
but if i use the greater than sign wouldn’t the move the biggest word  
into the sorted\_array?

i’m really new to this, so please pardon my misunderstanding.

---

<div class="post-metadata">

**Author:** ![johny.lam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@johny.lam](https://www.ruby-forum.com/u/johny.lam)\
**Post date:** [July 19, 2007, 11:12am UTC](https://www.ruby-forum.com/t/confused-need-help-with-a-sorting-program/109819/2 "2007-07-19T11:12:02Z")

</div>

johny.lam wrote:

> if (unsorted\_array[i+1] != nil) and (unsorted\_array[i] \<  
> unsorted\_array[i+1])  
> unsorted\_array[i], unsorted\_array[i+1] = unsorted\_array[i+1],  
> unsorted\_array[i]  
> end  
> […]  
> what i don’t get is that this program sorts the words into reverse  
> alphabetical order.

Ok, let’s look at what the above code does: It looks at an item in the  
array  
(unsorted\_array[i]) and at the one after it (unsorted\_array[i+1]). Then  
it  
checks whether the item is less than the next one and if so, it switches  
them.  
Ultimately this will result in an array in which every element is more  
than  
the element after it, i.e. an array sorted in descending order.

> but if i switch the less than sign in “if (unsorted\_array[i+1] != nil)  
> and (unsorted\_array[i] \< unsorted\_array[i+1])” to a greater than sign  
> the program works correctly.

Yes, because then it will switch the two items if the current item is  
more  
than the next one, which will result in an array in which every element  
is  
less than the next, i.e. an array sorted in ascending order, which is  
what  
you want.

> what i am trying to is move the smallest word into the sorted\_array  
> but if i use the greater than sign wouldn’t the move the biggest word  
> into the sorted\_array?

The code doesn’t create a new array. It just switches the items in the  
array  
around untill the array is sorted. Of course the items should only be  
switched  
if they are not already in the right order. So you check whether the  
items are  
in the wrong order (i.e. whether the current item is more than the next  
one)  
and if so, switch the items.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![johny.lam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@johny.lam](https://www.ruby-forum.com/u/johny.lam)\
**Post date:** [July 19, 2007, 11:14am UTC](https://www.ruby-forum.com/t/confused-need-help-with-a-sorting-program/109819/3 "2007-07-19T11:14:18Z")

</div>

johny.lam wrote:

> I was doing the recursive sorting program in Chapter 10 of Chris  
> Pine’s learning to program and i wrote this:
> 
> what i don’t get is that this program sorts the words into reverse  
> alphabetical order.

> what i am trying to is move the smallest word into the sorted\_array  
> but if i use the greater than sign wouldn’t the move the biggest word  
> into the sorted\_array?

Yes, that’s true. The problem here is that you are not calling the  
function recursively with sorted\_array. The sorted\_array is getting  
discarded at each level and replaced with [] each time, since you’re  
calling ‘sort’ instead of ‘recursive\_sort’.

There is a little illusion here, which you will see if you change the  
‘puts’ to a ‘p’. You are not actually getting an full array out the end,  
it just looks like you are because you are printing the elements one at  
a time.

You need to pass the sorted\_array down into the next level of  
recursive\_sort, by  
changing

unsorted\_array.pop  
sort(unsorted\_array)

to

unsorted\_array.pop  
recursive\_sort(unsorted\_array, sorted\_array)

Now this will print out the sorted array n times, since as each  
recursive\_sort returns, it will look at the now empty unsorted\_array and  
then print sorted\_array. I’d get rid of

if unsorted\_array.length==0  
p sorted\_array # \<-- was ‘puts’  
end

altogether and arrange it so that the sort function returns the sorted  
array:

def recursive\_sort unsorted\_array, sorted\_array  
…  
return sorted\_array  
end

…

# call the sort function

p sort(word)

best,  
Dan

---

<div class="post-metadata">

**Author:** ![johny.lam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@johny.lam](https://www.ruby-forum.com/u/johny.lam)\
**Post date:** [July 19, 2007, 11:33am UTC](https://www.ruby-forum.com/t/confused-need-help-with-a-sorting-program/109819/4 "2007-07-19T11:33:09Z")

</div>

Daniel L. wrote:

> Sebastian H. wrote:

> > The code doesn’t create a new array. It just switches the items in the  
> > array  
> > around untill the array is sorted. Of course the items should only be
> 
> Sorry to contradict you Sebastian:

Yeah, seems I didn’t read the code carefully enough. I only focussed on  
the  
part that I quoted.

> the original array isn’t sorted, but  
> cleared out by this code, since he’s calling unsorted\_array.pop at each  
> level.

But before it pops, it actually does sort the unsorted array in the  
0.upto  
part, doesn’t it?

---

<div class="post-metadata">

**Author:** ![johny.lam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@johny.lam](https://www.ruby-forum.com/u/johny.lam)\
**Post date:** [July 19, 2007, 11:18am UTC](https://www.ruby-forum.com/t/confused-need-help-with-a-sorting-program/109819/5 "2007-07-19T11:18:59Z")

</div>

Sebastian H. wrote:

> The code doesn’t create a new array. It just switches the items in the  
> array  
> around untill the array is sorted. Of course the items should only be

Sorry to contradict you Sebastian: the original array isn’t sorted, but  
cleared out by this code, since he’s calling unsorted\_array.pop at each  
level. It only looks like it ends up sorted because of the puts [“bar”]  
at the end of each call.

The ‘\<’ is actually the right way round if you going to do it this way  
and pop the last (smallest) element of the list each time and push it  
onto another array.

best,  
Dan

---

<div class="post-metadata">

**Author:** ![johny.lam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@johny.lam](https://www.ruby-forum.com/u/johny.lam)\
**Post date:** [July 19, 2007, 11:40am UTC](https://www.ruby-forum.com/t/confused-need-help-with-a-sorting-program/109819/6 "2007-07-19T11:40:13Z")

</div>

Sebastian H. wrote:

> Daniel L. wrote:
> 
> > Sebastian H. wrote:  
> > Yeah, seems I didn’t read the code carefully enough. I only focussed on  
> > the  
> > part that I quoted.

I actually wrote that post twice over :-). I spend too much time here…

> > the original array isn’t sorted, but  
> > cleared out by this code, since he’s calling unsorted\_array.pop at each  
> > level.
> 
> But before it pops, it actually does sort the unsorted array in the  
> 0.upto  
> part, doesn’t it?

Yep. If it wasn’t popped but just took the last element (and kept track  
of you place), you’d end up with a new array that was sorted correctly,  
and the original array would be sorted in place, in reverse.

This is all very verbal. Recursion is one of those things you have to  
expend an enormous amount of words to make your meaning understood.

best,  
Dan
