Don't try to memorize the code line for line. Try to understand the concept and the mechanism behind the algorithm. If you understand that you'll be able to re-piece it back together.
Is this for an interview? If it's general use just google it when you need