4

Since a sufficiently large hash table takes constant time to both insert and retrieve data, should it not be possible to sort an array by simply inserting each element into the hash table, and then retrieving them in order?

You just insert each number into the hash table, and remember the lowest and highest number inserted. Then for each number in that range, in order, test if it is present in the hash table.

If the array being sorted contains no gaps between values (i.e. it can be [1,3,2] but NOT [1,3,4]), this should give you O(N) time complexity.

Is this correct? I don't think I've ever heard of hash tables being used this way - am I missing something? Or are the restrictions (numeric array with no gaps) too much for it to be practically useful?

Raphael
  • 72,336
  • 29
  • 179
  • 389
Benubird
  • 173
  • 1
  • 5
  • @Raphael 1) They are not ordered, but if you require the the only data stored in it is a series of numbers, and you know the min and max number, then you know all of the entries present (although you also "know" some that might not be). Right, they have O(N)... wait, I see what you're saying - O(N) N times is N^2... hm. Maybe I did misunderstand that? – Benubird Jun 18 '15 at 13:22
  • @Raphael Ok, apparently hash tables CAN have O(1) worst case: http://cs.stackexchange.com/a/43192/34565 – Benubird Jun 18 '15 at 13:37
  • David explains why that particular approach fails, no matter which set representation you use. 2) Exactly. 3) That question only claims O(1) lookup, whereas you need all the other operations, too!
  • – Raphael Jun 18 '15 at 14:28
  • 1
    When you are dealing with numbers (represented in some base) then you can sort in linear time, for example with radix sort. So a linear time sorting algorithm for the case you presented wouldn't be earth-shattering, even if it was correct. – Tom van der Zanden Jun 18 '15 at 14:34