Now that we have a definite way of writing our algorithms, let's look at some algorithms for solving the problem of sorting. Sorting is a very common problem handled by computers. For example, most graphical email programs allow users to sort their email messages in several ways: date received, subject line, sender, priority, etc. Each time you reorder your email messages, the computer uses a sorting algorithm to sort them. Since computers can compare a large number of items quickly, they are quite good at sorting.
During the next few lessons, you will learn three different algorithms for sorting: the Simple Sort, the Insertion Sort, and the Selection Sort. These algorithms will give us a basis for comparing algorithms and determining which ones are the best. We will illustrate each algorithm by sorting a hand of playing cards like the ones below. Traditionally, a group of playing cards is called a "hand" of cards. In the next few lessons, we will use the term "hand" to refer to a group of seven playing cards.
Then we will show how the algorithm also applies to the problem of sorting a list of numbers in a computer. Our list of numbers will be stored in an array of memory cells like the diagram below.
It is important to note that we will be using the same algorithm to sort both playing cards and numbers. One important quality of a good algorithm is that it solves a class of problems and not just one particular problem. A good sorting algorithm should provide a solution to the problem of sorting for many types of items. Imagine if you were trying to design an email program that allowed users to sort their messages by date received, subject line, and sender. Would you want to write a new algorithm for each different sort, or would you prefer to write a single algorithm that handled all three? Of course you would prefer the latter approach, so when you designed your algorithm, you would design it to solve a class of problems (e.g. sorting) rather than an individual problem (e.g. sorting emails by date).