Sorting arrays

Sorry I'm not trying to be frustrating.

def sort(list)
sorted =
unsorted =

lowest = nil <----------- here lowest is being set to nil
list.each do |item|
if lowest == nil || item < lowest <----then expression 1 is true (as you
say above).

lowest = item
end

I might just be missing a brain, because it seems like it's not sinking
through.

Stuart

···

On 7/9/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

I don't see that in your code. I see this:

   lowest = nil

So this:

   lowest == nil

is true.

Hi --

I don't see that in your code. I see this:

   lowest = nil

So this:

   lowest == nil

is true.

Sorry I'm not trying to be frustrating.

def sort(list)
sorted =
unsorted =

lowest = nil <----------- here lowest is being set to nil
list.each do |item|
if lowest == nil || item < lowest <----then expression 1 is true (as you
say above).

Correct. Therefore this gets executed:

lowest = item

So now, lowest is item -- which was "five" (or whatever the first
array item was).

So *next* time through the loop, this:

   lowest == nil

will *not* be true. So this:

   item < lowest

will be tested instead.

David

···

On Mon, 10 Jul 2006, Dark Ambient wrote:

On 7/9/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS, the Ruby book for
                                                     Rails developers
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net => me

David,

Thank you for your patience. I now get it! As you mentioned previously
the first time through your loop, lowest is nil -- which means that the
item < lowest comparison is never evaluated, second iteration (via lowest =
item) changes it.
Why I didn't see it then.

Apologies
Stuart

···

On 7/9/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Mon, 10 Jul 2006, Dark Ambient wrote:

> On 7/9/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
>
>> I don't see that in your code. I see this:
>>
>> lowest = nil
>>
>> So this:
>>
>> lowest == nil
>>
>> is true.
>
> Sorry I'm not trying to be frustrating.
>
> def sort(list)
> sorted =
> unsorted =
>
> lowest = nil <----------- here lowest is being set to nil
> list.each do |item|
> if lowest == nil || item < lowest <----then expression 1 is true (as
you
> say above).

Correct. Therefore this gets executed:

> lowest = item

So now, lowest is item -- which was "five" (or whatever the first
array item was).

So *next* time through the loop, this:

   lowest == nil

will *not* be true. So this:

   item < lowest

will be tested instead.

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS, the Ruby book for
                                                     Rails developers
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net => me

I'm back already :). Basically I'm trying to take James original recursion
version and do it with recursion. Anyway, my questions are below in the
<---------------- lines.
TIA
Stuart

def sort(list)
  sorted = []
  unsorted = []
  puts 'here is list: '
  puts list <------ Here i'm outputing the list I put in to the method -
prints out the entire list
  puts

  lowest = nil
  list.each do |item|
    if lowest == nil || item < lowest

      lowest = item
    end
  end
  sorted.push(lowest)

  # make a new unsorted list, minus the low guy
  unsorted = [ ]
  added = false
  list.each do |item|
    if added == false and item == lowest
      added = true
    else
      unsorted.push(item)
    end
  end

  puts 'Here is the unsorted list'
  puts unsorted
  puts
  puts 'Here is the sorted list'
  puts sorted
  puts
  puts 'Here is list' <----------------------- Here though, the list is
empty (at least of values). Something I don't get, since I didn't see any
.delete
  puts
  puts 'list size is: '<---------------------------yet here the size
returned is 5 the number of original elements.
  puts list.size

  # third step (from James' is where he returns control to method (or back
to recursion)
  #if list.size == 0
   # sorted
  #else
   # recursive_sort(unsorted, sorted)
  #end
end #end of method

mylist = ['ten', 'eight', 'one', 'six', 'five']

sort mylist