Dark Ambient wrote:
> Actually it's from Chris Pine's book, Learning to Program.  I thought
> it was a bit fuzzy in the book, not so much my understanding but the
> suggestions he makes to accomplish the task.  His idea is to have two
> additional lists, or arrays , one called "already sorted", and the
> other "unsorted".  I'm just being repetetive here to make the point of
> where his advice is fuzzy.  It reads as if you would use the first
> array (where the strings were put) and test those against themselves
> to find the smallest one, where the smallest (one that returns true)
> goes into the sorted, repeat and the ones that return false go into
> unsorted.  Then the next iteration , or final stage is testing the
> sorted list against the unsorted list.
>
> Using "insertion sort" it seemed that things were moved around in just
> the initial array.
>
> Stuart
>
Oh, okay, sorry for my misunderstanding! I didn't pay attention to who
That's not really insertion sort, no...here's one way to do it

#if you can use Array#min:

list = ["aword", "cword", "gword", "zword", "dword", "qword", "pword"]
unsorted = []
sorted = []

sorted << list.min                  #add the smallest to sorted
unsorted = list - sorted      #add the rest to unsorted

until unsorted.empty?      #when unsorted is empty, we are done
sorted << unsorted.min      #put the smallest value into sorted
unsorted.delete(unsorted.min)   #take it out of unsorted
end

p sorted   #all sorted!

--------------------------------------
#if you can't use Array#min:

list = ["aword", "cword", "gword", "zword", "dword", "qword", "pword"]
unsorted = []
sorted = []

unsorted = list.dup       #make a copy of the list of words so we don't
destroy the original!

until unsorted.empty?     #when unsorted is empty, we are done

min = unsorted[0]         #get our initial 'minimum value'

unsorted.each do |word|
if word < min                      #check the rest of the values
against it
min = word                      #if we find something smaller,
that's our new min
end
end

sorted << min               #put the smallest in sorted
unsorted.delete(min)     #delete it from unsorted
end

p sorted   #all sorted!

------------------------------------

The comments should help explain what's going on. Note that I skip the
first step ("get the smallest value from the original list, put that in
sorted, put the rest in unsorted") in the second example, because I copy
the entire list into unsorted first.

Anyways, I hope that helps. You can probably come up with a better version!

-Justin