Skip to content

Instantly share code, notes, and snippets.

@rvbsanjose
Created August 24, 2012 07:19
Show Gist options
  • Save rvbsanjose/3447052 to your computer and use it in GitHub Desktop.
Save rvbsanjose/3447052 to your computer and use it in GitHub Desktop.
Median
def median(array)
sorted_array = array.sort
if array.length % 2 != 0
index = sorted_array.length / 2
median = sorted_array[index]
else
index = sorted_array.length / 2
median = (sorted_array[index].to_f + sorted_array[index-1].to_f) / 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment