Algorithm
From Simple English Wikipedia, the free encyclopedia
An algorithm is the directions that a computer or many computers follow in computer language. It tells a computer what to do, step by step.
In cooking, some recipes are harder to do than others, because they take more time to finish or have more things to keep track of. It's the same for algorithms, and algorithms are better when they are easier for the computer to do. The thing that measures how hard an algorithm is is called complexity.
[edit] Sorting by colors
This is an example of an algorithm for sorting a stack of cards with colors on them into piles of the same color:
- Take a card from the stack and look at its color.
- If there is already a stack of cards of that color, put the card on the stack with the same color.
- If there is no stack with cards of that color, make a new stack with that color, and put the card on it.
Do this algorithm until there are no cards left in your hand.
[edit] Sorting by numbers
This is an example of an algorithm for sorting a stack of cards with many different numbers, so that the numbers are in order.
You start with a stack of cards that haven't been sorted. Call this "stack A". You're also going to make a "stack B", but it starts with no cards in it. Then follow these steps:
- Take a card from stack A.
- If there are no cards in stack B, put the card you took in stack B.
- If you still have the card, see if it has a higher or lower number than the card on the top of stack B.
- If your card has a higher number than the one on top of stack B, put the card on top of stack B.
- If your card has a lower number than the one on top of stack B, or if the numbers are equal, put your card under the top card on stack B, and remember that you did this.
When you run out of cards in stack A:
- If you didn't put any cards under another card when you followed those steps, you're done.
- If you did, move stack B to stack A and start over.
[edit] Putting algorithms together
If you have cards with colors and numbers on them, you can sort them by color and number if you do the "sorting by colors" algorithm, then do the "sorting by numbers" algorithm to each colored stack, then put the stacks together.
The sorting by numbers algorithm is harder to do than the sorting by colors algorithm, because you may have to do the steps again many times. You would say that sorting by numbers is more complex.