implemented algorithm C