fish = ['fish', 'fiiish', 'fiiiiish', 'fiiiish', 'fffish', 'ffiiiiisshh', 'fsh', 'fiiiissshhhhhh']

def bubble_sort(array) # sluggish
  return_array = array.dup
  swapped = true

  while swapped
    swapped = false
    (0...return_array.length-1).each do |idx|
      if return_array[idx].length > return_array[idx+1].length
        swapped = true
        return_array[idx], return_array[idx+1] = return_array[idx+1], return_array[idx]
      end
    end
  end

  return_array
end

def sluggish(array)
  bubble_sort(array)[-1]
end

def merge(array_1, array_2)
  return_array = Array.new

  while (array_1.length + array_2.length) > 0
    if array_1.length == 0
      return_array.append(array_2.shift)
    elsif array_2.length == 0
      return_array.append(array_1.shift)
    else
      return_array.append((array_1[0].length <= array_2[0].length ? array_1.shift : array_2.shift))
    end
  end

  return_array
end

def merge_sort(array)
  return array if array.length <= 1
  middle_idx = array.length / 2
  return merge(merge_sort(array[0...middle_idx]), merge_sort(array[middle_idx..-1]))
end

def dominant(array)
  merge_sort(array)[-1]
end

def clever(array)
  highest = 0
  return_value = nil

  array.each do |word|
    if word.length > highest
      highest = word.length
      return_value = word
    end
  end

  return_value
end

p fish
p sluggish(fish)
p dominant(fish)
p clever(fish)