implement of algorithm three

having some ideas about the implement.