In this example code, the PDL comments have been highlighted
to help you identify them more easily.

/*****************************************************************The purpose of a swap sort is to put an array of numbers into sequential order. This sorting is accomplished by comparing the values of two adjacent values in the array. If the two numbers are already in proper numerical order, the numbers remain in position. If the two adjacent numbers are out of proper sequence, their positions are interchanged.The function needs to know the content of an array of numbers and the count of numbers in that array.Last modification done on: June 6, 2000. The output of this program should look like this: Contents of original array: 8 3 5 1 Contents of 'swapped' array: 1 3 5 8 Press any key to continue

/*****************************************************************/ #include <iostream.h> void main() { int Temp; int MAX = 4; int ArrayToSort[4] = {8, 3, 5, 1};// Display contents of the original arraycout << "Contents of original array: \n" ; for (int First = 0; First < MAX; First++) { cout << ArrayToSort[First] << " "; } cout << endl;// Loop, counting down from the count of numbers to 1for (int i = (MAX-1); i > 0; i--) {// Loop, counting from 1 to the current value of the outer loop counterfor (int j = 0; j < (MAX-1); j++) {// (Assuming the value of the inner loop index is n:)// If the nth element in the array is larger than the (n+1)th elementif (ArrayToSort[j] > ArrayToSort[j + 1]) {// Assign the nth array element to a temporary variableTemp = ArrayToSort[j];// Assign the (n+1)th array element to the nth array elementArrayToSort[j] = ArrayToSort[j+1];// Assign the temporary variable to the (n+1)th element in the arrayArrayToSort[j+1] = Temp; }// End if}// End loop}// End loop// Display contents of the sorted arraycout << "\nContents of 'swapped' array: \n" ; for (int newFirst = 0; newFirst < MAX; newFirst++) { cout << ArrayToSort[newFirst] << " "; } cout << endl << endl; }

Created by
Allan Caine,
Laura Drever,
Gorana Jankovic,
Megan King,
and
Marie Lewis.

Revised by
Vince Sorensen
and
Mandy Aldcorn.

Last Modified: June 8, 2000

*Copyright 2000 Department of Computer Science,
University of Regina.*