Swap Sort in C With PDL

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. Output should look like this:

Contents of array before sort

8 3 5 1 Contents of array after sort: 1 3 5 8 ***************************************************************** */

#define MAX 4

int main() { int i; int j; int x = 0; int temp=0; int array[] = {8, 3, 5, 1};/* Display contents of the original array */printf("\nContents of array before sort:\n"); for (x=0; x < MAX; x++) { printf("%i ", array[x]); }/* Loop, counting down from the count of numbers to 1 */for (i=(MAX-1); i > 0; i--) {/* Loop, counting from 1 to the current value of the outer loop counter */for (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 element */if (array[j] > array[j+1]) {/* Assign the nth array element to a temporary variable */temp = array[j];

/* Assign the (n+1)th array element to the nth array element */ array[j] = array[j+1];/* Assign the temporary variable to the (n+1)th element in the array */array[j+1] = temp;

}/* End if */}/* End loop */}/* End loop *//* Display contents of the sorted array */printf("\n\nContents of array after sort:\n"); for (x=0; x < MAX; x++) { printf("%i ", array[x]); } printf("\n\n"); return 0; }

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.*