Consider the following algorithm to sort the entries in a stack S1. First create two empty stacks,
Fantastic news! We've Found the answer you've been seeking!
Question:
Consider the following algorithm to sort the entries in a stack S1. First create two empty stacks, S2 and S3. Move the top of S1 to S2. Now, at any given time, stack S2 holds the entries in sorted order, with the smallest at the top of the stack. Consider the top t of S1. Pop entries of stack S2 and push them onto Stack S3 until you reach the correct place to put t.. Then push t onto S2. Next move all of the entries from S3 to S2.
Implement this algorithm both recursively and iteratively.
Related Book For
Data Structures and Algorithm Analysis in Java
ISBN: 978-0132576277
3rd edition
Authors: Mark A. Weiss
Posted Date: