Today I learned a sorting algorithm, this program takes a list of words and sorts them according to how they would appear in a dictionary.

E.g

cat,dog,horse

So first of all, there is an array

**unsorted,**this contains all the unsorted words.Next we take out one word (called

**smallest)**from the array and use a**do**method to compare it with all the other words in the**unsorted**array. (in this case called**tested_object**)If the

**tested_object**is smaller than the**smallest,**then the**smallest**is placed into another array.**still_unsorted**array. and the**tested_object**becomes the new**smallest**If the

**tested_object**is larger than the**smallest**, than the**tested_object**is placed in the**still_unsorted**arrayBecause we iterate through the entire

**unsorted**array, the last**smallest**remaining will actually be the “smallest” (this means it will appear first in the dictionary)We than use recursion by using the same

**method**as above only this time we use**still_unsorted**instead of**sorted**Below is an example code (

**taken from Learning to Program Ed.2**)def recursion_array(unsorted sorted)

smallest = sorted.pop

still_unsorted = []

unsorted.each do |tested_object|

if tested_object < smallest

still_unsorted push smallest

smallest = tested_object

else

still_unsorted.push tested_object

end

sorted.push smallest

recursion_array(still_unsorted sorted)

end

This is an example of recursion, ~~which is the act of cursing over and over~~, which means the program is calling a method repeatedly, but each method call brings the loop closer to the answer/intended output/solution.

I know this has been wildly exciting, stay tuned as I delve into even more exciting algorithms, and maybe one day even be able to publish code in a semi-decent way.

Advertisements