From: Dark Ambient Date: 2006-07-09T06:41:15+09:00 Subject: Re: Sorting arrays I have a question on the code below that James shared with me. While he might be best to answer if anyone has an idea please feel free to reply. I'm looking in particular , def recursive_sort. The list is first passed into def sort which immediately puts it into the recursive_sort. The two parameters are unsorted (which I understand) and the [] which I don't understand. I think *maybe* this is taking the first element of the unsorted array and putting it into sorted. Is that correct ? TIA Stuart On 6/29/06, James Edward Gray II wrote: > def sort(array) > recursive_sort(array, []) > end > > def recursive_sort(unsorted, sorted) > # find the lowest element > lowest = nil > unsorted.each do |item| > if lowest == nil || item < lowest > lowest = item > end > end > > # add it to our sorted list > sorted.push(lowest) > > # make a new unsorted list, minus the low guy > new_unsorted = [ ] > added = false > unsorted.each do |item| > if added == false and item == lowest > added = true > else > new_unsorted.push(item) > end > end > > # return sorted list if we are done, or recurse to keep sorting > if new_unsorted.size == 0 > sorted > else > recursive_sort(new_unsorted, sorted) > end > end > > list = ['zeta', 'beta', 'alpha', 'beta'] > puts sort(list) > > __END__ > > Hope that helps. > > James Edward Gray II > > > >