ELI5: Explain Like I'm 5

Shunting yard algorithm

Shunting yard algorithm is like sorting toys into their right toy boxes. You're given a list of toys (numbers and operators like + and -), and a stack of toy boxes to put them in (queue). It helps you figure out the correct order to put the toys in the toy boxes, so that you can get the right answer.

For example, if you had the toys 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3, you would start by looking at the first one, which is the number 3. You would put it in the queue.

Next, you would look at the next toy, which is the operator +. You don't know where to put it yet, so you put it on a separate stack called the operator stack.

Now you look at the next toy, which is the number 4. You put it into the queue.

You keep going like this, putting operators on the operator stack, and numbers in the queue, until you get to the end of the list.

At this point, you'll have a stack of operators that are still waiting to be put in the queue. You can use the rules of the algorithm to figure out which operators go where.

For example, if you have an operator on your stack that has a lower precedence level than the one on top of the stack, you know that the one on top of the stack needs to be put in the queue first.

Once you've sorted out where all the operators go, you can take them off the stack and put them in the queue in the right order.

Finally, you'll have a queue with all the numbers and operators in the correct order to get the right answer to your mathematical problem.
Related topics others have asked about: