Algorithms for Memory Hierarchies