<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in 'Sort using recursion (10.2 Rite of Passage: Sorting)' | Pragmatic Forums</title>
    <link>http://forums.pragprog.com/forums/6/topics/201</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <description></description>
    <item>
      <title>Sort using recursion (10.2 Rite of Passage: Sorting) posted by Tyler Bird @ Fri, 11 Jan 2008 22:17:11 -0000</title>
      <description>&lt;p&gt;I was on the exact same issue and I&amp;#8217;m so pleased with myself that I figured it out I thought I&amp;#8217;d share.&lt;/p&gt;


	&lt;p&gt;Keep in mind, that if you want to solve it yourself this gives you the answer and you should disregard the reply&amp;#8230;&lt;/p&gt;


	&lt;p&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: Remember to uncomment the method you&amp;#8217;d prefer to run in the &lt;code&gt;sort&lt;/code&gt; method.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
def sort an_array
  #recursive_sort an_array, [], 0
  #iterative_sort an_array, []
end

def iterative_sort unsorted_array, sorted_array

    i = 0
    loop do

      break if unsorted_array.length == 0

      score     = 0                      
      length    = unsorted_array.length  
      this_word = unsorted_array[i]      

      # count each time this_word is the smallest &amp;#38; count itself once
      unsorted_array.each do |array_word|
        if this_word &amp;lt;= array_word
          score += 1
        end
      end

      # this_word is the smallest when score == length      
      if score == length
        sorted_array.push(this_word)
        unsorted_array.delete(this_word)
        i = 0  # start at the beginning of the array next time.
      else
        i += 1 # increment the array to check the next word.
      end

    end

  sorted_array
end

def recursive_sort unsorted_array, sorted_array, i

  return if unsorted_array.length == 0

    score     = 0                      
    length    = unsorted_array.length  
    this_word = unsorted_array[i]      

    # count each time this_word is the smallest &amp;#38; count itself once
    unsorted_array.each do |array_word|
      if this_word &amp;lt;= array_word
        score += 1
      end
    end

    # this_word is the smallest when score == length      
    if score == length
      sorted_array.push(this_word)
      unsorted_array.delete(this_word)
      i = 0
    else
      i += 1 # increment the array to check the next word.
    end

  recursive_sort unsorted_array, sorted_array, i

  sorted_array
end

messy_array = ['alpha', 'echo', 'delta', 'beta', 'charlie']

puts sort(messy_array)
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;</description>
      <pubDate>Fri, 11 Jan 2008 22:17:11 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:6:201:2156</guid>
      <author>Tyler Bird</author>
      <link>http://forums.pragprog.com/forums/6/topics/201</link>
    </item>
    <item>
      <title>Sort using recursion (10.2 Rite of Passage: Sorting) posted by Leo Baghdassarian @ Sat, 05 Jan 2008 02:28:20 -0000</title>
      <description>&lt;p&gt;Only using the methods from the first 10 chapters of the book, the solution hasn&amp;#8217;t come to me.   What have you come up with?&lt;/p&gt;</description>
      <pubDate>Sat, 05 Jan 2008 02:28:20 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:6:201:2117</guid>
      <author>Leo Baghdassarian</author>
      <link>http://forums.pragprog.com/forums/6/topics/201</link>
    </item>
  </channel>
</rss>
