Hi Mark,

Just committed and pushed (my) exercise.
I modified your code step by step in each commit and left comments so
that you can follow the reasoning of each modification.

Your code turned into a pure bubble sort implementation bellow.
Believe me... this is a patch to your initial code! :-)
It behaves (almost) exactly the same as your initial code.

words = []

words.push gets.chomp while words.last != ''

words.pop

begin
  more = false
  1.upto(words.length-1) do |word_ix|
    if words[word_ix-1] > words[word_ix]
      words[word_ix-1], words[word_ix] = words[word_ix], words[word_ix-1]
      more = true
    end
  end
end while more

puts
puts words
puts

It's pushed to https://github.com/abinoam/word_sort

Abinoam Jr.

Hint: (as Rafael said)
http://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation

On Wed, Aug 14, 2013 at 1:47 PM, Rafael M. <lists / ruby-forum.com> wrote:
> I think you should take a look at some methods of sorting and choose
> which you think is the best.
> http://en.wikipedia.org/wiki/Sorting_algorithm
> http://www.youtube.com/watch?v=kgBjXUE_Nwc
>
> I chose bubble because it is the simplest and I came up with this, what
> do you think?:
>
>   1 class WordList
>   2   @words
>   3
>   4   def initialize
>   5     @words = []
>   6   end
>   7
>   8   def add word
>   9     @words.push word
>  10   end
>  11
>  12   def show
>  13     @words
>  14   end
>  15
>  16   def sort
>  17     sorted = @words
>  18     length = sorted.length
>  19
>  20     begin
>  21       swap = false
>  22
>  23       sorted.each_index do |e|
>  24         next_key = e + 1
>  25
>  26         next if next_key >= length
>  27
>  28         if sorted[e] > sorted[next_key]
>  29           sorted[e], sorted[next_key] = sorted[next_key], sorted[e]
>  30           swap = true
>  31         end
>  32       end
>  33     end while swap
> 34
>  35     sorted
>  36   end
>  37 end
>  38
>  39 class Main
>  40   @words
>  41
>  42   def initialize
>  43     @words = WordList.new
>  44
>  45     write_some_words
>  46     show_words
>  47     show_sorted
>  48   end
>  49
>  50   def write_some_words
>  51     msg = "Type something:"
>  52
>  53     puts msg
>  54
>  55     while((word = gets.chomp) != '') do
>  56       puts msg
>  57       @words.add word
>  58     end
>  59   end
>  60
>  61   def show_words
>  62     p @words.show
>  63   end
>  64
>  65   def show_sorted
>  66     p @words.sort
>  67   end
>  68 end
>  69
>  70 Main.new
>
> --
> Posted via http://www.ruby-forum.com/.