Now that you have seen all the pieces of programming languages that make up a typical computer program, let's consider how we put these together to make a program. You are already familiar with the notation used to describe subprograms (procedures and functions), and the notation for a program is really just the same. First, we begin by using the keyword Program and EndProgram to indicate which statements belong to the program. These keywords also tell the computer that this part is the start of our code and should be executed first. Next, we declare the variables that the program will use for storing and processing data. Last, we implement the logic of the program using the three control structures and the language's I/O procedures. The basic outline of a program is given below.

 Program MyProgram ()               .       .       .           .       .       . EndProgram

Now let's take a look at a complete program that implements a sorting algorithm called Selection Sort. The box below gives a description of the Selection Sort algorithm.

 Selection Sort Algorithm Get an array of unsorted numbers Set a marker for the unsorted section at the front of the array Repeat steps 4 - 6 until one number remains in the unsorted section    Compare all unsorted numbers in order to select the smallest one    Swap this number with the first number in the unsorted section    Advance the marker to the right one position Stop (For more information, see The Selection Sort in the Algorithms module.)

In order to implement this algorithm, we are going to use a program called Sort and the subprograms SelectionSort, FindSmallest, and Swap. Each of these parts is listed below with a short description of their purpose. To view the implementation of an individual part in a new window, click on the name.

Sort:
The Sort program is responsible for inputting an array of numbers to be sorted, calling the SelectionSort subprogram to order the numbers, and outputting the sorted array.

SelectionSort:
The SelectionSort procedure is responsible for ordering an array of numbers using the Selection Sort algorithm. This procedure calls FindSmallest and Swap as part of the sorting algorithm.

FindSmallest:
The FindSmallest function searches a range of cells in an array and returns the location of the smallest number within the range.

Swap:
The Swap function exchanges the values of two variables in memory

Now we are ready to trace the execution of the program to see exactly how it works. Click on the button below to start the Trace Sort applet. Using the controls at the top of the applet, step through the program so that you can understand the flow of control and the variable assignments.