NVM, I saw your other comment with the specific sorting question in it, and I get it. It's a question about "are you familiar with or can you come up with this 1 weird trick" rather than "can you implement a sorting algorithm". Yeah, I wouldn't really expect anyone I work with to come up with that solution, and while it's theoretically slower, I just tested them both, and the builtin sort was over twice as fast, and way more obvious.
ruby -e'
def time
start = Time.now
yield
Time.now - start
end
def sort(a)
counts = Array.new 100, 0
a.each { |n| counts[n] += 1 }
sorted = Array.new a.size
i = -1
counts.each_with_index do |count, index|
count.times { sorted[i += 1] = index }
end
sorted
end
a = 1_000_000.times.map { rand 0...100 }
puts correct: a.sort == sort(a),
builtin: time { a.sort },
custom: time { sort a }
'{:correct=>true, :builtin=>0.039097, :custom=>0.087166}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
NVM, I saw your other comment with the specific sorting question in it, and I get it. It's a question about "are you familiar with or can you come up with this 1 weird trick" rather than "can you implement a sorting algorithm". Yeah, I wouldn't really expect anyone I work with to come up with that solution, and while it's theoretically slower, I just tested them both, and the builtin sort was over twice as fast, and way more obvious.