Homework, huh? And not the first time you asked, apparently.
If you look,
on the Internet you can find absolutely any algorithm you need. But if you are learning .. or supposed to be learning .. then you'll only cheat yourself.
Furthermore, if you wait until the last minute on anything,
well, I guess that's the first
thing you need to "learn" about.