Explain how you would optimize a sorting algorithm to run efficiently on a device with highly constrained memory.