With a million keys, a straight scan of an array averages five hundred thousand comparisons, a balanced binary search tree takes about twenty, and a hash table jumps to the right bucket in a single move. The catch is real: feed the tree already-sorted increasing data and every new value falls to the right, the tree stretches into a single chain n levels deep and search sinks back to O(n). The fuller a hash table gets, the more keys land in the same bucket, the longer the chain inside it, and the more that O(1) holds only on average. A heap keeps every parent below its children throughout, so the root is always the smallest value.
With a million keys, a straight scan of an array averages five hundred thousand comparisons, a balanced binary search tree takes about twenty, and a hash table jumps to the right bucket in a single move. The catch is real: feed the tree already-sorted increasing data and every new value falls to the right, the tree stretches into a single chain n levels deep and search sinks back to O(n). The fuller a hash table gets, the more keys land in the same bucket, the longer the chain inside it, and the more that O(1) holds only on average. A heap keeps every parent below its children throughout, so the root is always the smallest value.
Related topics in Mind & Machine