Related questions with answers
Compare the two solutions to the shampooing algorithm shown. Which do you think is a better general-purpose solution? Why? (Hint: What if you wanted to wash your hair 1,000 times?)
Solution
VerifiedThe algorithm from Fig 1.3 contains an iterative operation (repeating a number of steps) and a value to keep track of how many times the loop should be performed.
The Fig 1.4 algorithm simply has steps which look exactly the same and take extra space while not providing enough new information. One would only have to read the 3 steps and count how many times the same steps are written which is what is for in the second algorithm.
The first one is more efficient and elegant since it would look the same regardless of how many times the loop has to be performed. Only the value of would change.
Create an account to view solutions
Create an account to view solutions
Recommended textbook solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface
5th Edition•ISBN: 9780124077263David A. Patterson, John L. Hennessy
Fundamentals of Database Systems
7th Edition•ISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
Introduction to Algorithms
3rd Edition•ISBN: 9780262033848 (5 more)Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
Invitation to Computer Science
7th Edition•ISBN: 9781305075771G Michael Schneider, Judith L. GerstingMore related questions
1/4
1/7