My sort of sort
Am running through “Learn to Program” from Chris Pine, again.
I have been cracking my head on the exercise to write a sort method instead of just using the array#sort come with ruby.
As the exercise actually came at the stage of the tutorial that more advanced techniques like ternary operator is yet to be introduced, so I figure I might want to limit myself to use only the basic while and if.There are some blog posts, discussion in forums on the solution to this question.
Someone suggest to read about sorting algorithm before attempting to solve the question, a brief search yields all sorts of sort (ha!), insertion, selection, bubble, shell, merge, heap, quick, just for sorting. As sorting always require brain gymnastic trying to imagine the on going process (at least for me), there are animations available for all these different type of sort. Although, frankly I still couldn’t really get a grasp after watching those animations.
As I read from Renae’s blog that they are using paper cutouts, while I have been sketching and sketching and sketching trying to illustrate the flow.
And so, here is my attempt to the problem, it is rather coarse, it didn’t use method, it can’t deal with duplicate elements in the array, so, there are still a lot of be improve on, when I have time on it. Nevertheless, here it is
animals = ['cat','bird','a','mouse','bull','elephant','dog'] unsorted = animals.dup sorted=  i = animals.length - 1 smaller = animals[i] while unsorted.length != 0 while i >= 0 if smaller < animals[i] smaller else smaller = animals[i] end i = i - 1 end sorted.push smaller unsorted.delete smaller animals.delete smaller smaller = unsorted[i] i = unsorted.length - 1 end puts sorted.inspect