Sunday, July 19, 2009

Hint #4 for my_sort

Hi,

Here's the 4th hint.

To check to see if a given element in an array is a lower value than the previous element, you'd do this:

a = [3, 2, 1]

puts a[2] < a[1] # true

With these hints, it should be possible to write a method that iterates backwards over an array and swaps each element with its previous element if the element is less than the previous element. If you do that iteration enough times, you will eventually have a sorted array. This is called a bubble sort in computer science.

Glenn

No comments:

Post a Comment