Algorithms

Here are some 3x5 cards with numbers printed on them:


Could you sort them into ascending order?

Sure you could -- it was easy. But, how did you do it? Could you write down the steps you followed? Could you write them so carefully that someone else could follow them and get the same result? Could you write them so carefully that a computer could follow them and get the same result? Such a list of steps would be called an algorithm.

This is the algorithm I used in sorting the cards into ascending order:

  1. look through the cards to find the smallest one
  2. set it aside, face up
  3. look through the remaining cards to find the smallest one
  4. put it under the one I just set aside, face up
  5. look through the remaining cards to find the smallest one
  6. put under the ones I have set aside, face up
  7. look through the remaining cards to find the smallest one
  8. put under the ones I have set aside, face up
  9. put the remaining card face up under the ones I have set aside
  10. stop
My algorithm is not all that clever, but it worked. I can imagine other algorithms that would produce the same result. They might even be faster to execute or shorter and simpler to write down.

We have been speaking of an algorithm for sorting numbers, but one can think of algorithms for all sorts of things. Could you write a list of steps for baking a chocolate cake or for getting up and getting ready to go to school or work in the morning?

One approach to designing an algorithm is to solve a problem yourself, while paying careful attention to how you do it. Instead of just sorting the cards in one step, introspect -- think about what you are doing as you do it, and write the steps down as you work. If you are making a cake, write each step as you complete it.

Do not try to write the steps in VB. Write them in English, using common sense vocabulary as I have done above. Once you understand the procedure you have been using to solve sample problems well enough to write the steps in English, you can translate them into VB.

Working through a problem yourself, and thinking about what you are doing, often helps in figuring out what exactly you want to tell the computer to do when an event occurs. Programming involves inventing procedures for the computer to follow, and a programmer must learn to think procedurally.

When you write down the steps of an algorithm in common-sense English, you are writing pseudo code. Once you have written and understand your pseudo code, you can code it in VB or a different programming language.

The term "algorithm" is derived from the name of the mathematician Abu Ja'far Muhammad ibn Musa Al-Khwarizm who lived in Baghdad in the ninth century. He is credited with the invention of algebra, also named for him, and the idea that math problems could be solved by a series of steps.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.