Code of the book Algorithm version 4