hw3 algorithm