222k views
2 votes
Write a method isSorted that accepts a stack of integers as a parameter and returns true if the elements in the stack occur in ascending (non-decreasing) order from top to bottom, and false otherwise.

1 Answer

6 votes

Step-by-step explanation:

bottom [20, 20, 17, 11, 8, 8, 3, 21 top the following stack is not sorted (the 15 is out of place), so passing it to your method should return a result of false: bottom [18, 12, 15, 6, 11 top an empty or one-element stack is considered to be sorted. when your method returns, the stack should be in the same state as when it was passed in. in other words, if your method modifies the stack, you must restore it before returning.

User Eric Stermer
by
5.4k points