Skip to content

Instantly share code, notes, and snippets.

@pricees
Created July 24, 2012 06:17
Show Gist options
  • Save pricees/3168338 to your computer and use it in GitHub Desktop.
Save pricees/3168338 to your computer and use it in GitHub Desktop.
The Counting Sort
module Algoruby
module Sort
#
# === Counting Sort ===
#
# Best: O(n) [linear]
# Avg: O(n) [linear]
# Worst: O(n) [linear]
#
module Counting
def self.sort(ary)
buckets = ary.inject(Array.new) do |tmp, n|
tmp[n] ||= 0
tmp[n] += 1
tmp
end
idx = 0
buckets.each_with_index do |n, i|
n && n.times do
ary[idx] = i
idx += 1
end
end
ary
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment