merge and find algorithm