In the main program, we need an array declaration, for example,
const int ARRLIMIT = 20; int somearray[ARRLIMIT];The following code is typed according to the type of the array somearray which is declared to be int. The code needs appropriate modifications if the array is of some other type. We need both a "switching" procedure and a "sorting" procedure (or we could integrate the switch at the appropriate place in the body of the "sorting" code).
void IntSwitch(int & first, int & second) { int temp; temp = first; first = second; second = temp; } void IntBubbleSort (int oarr[], int limit) { // This subprogram sorts an integer array // via bubblesort. (version Ford & Topp 783-84) // oarr is the input array to be sorted // -- it is of type int // limit is the ACTUAL number of items // stored in oarr -- it may be // less than the declared size of oarr int i,j; // loop indices int lastExchangeIndex; // value of index of // last element exchanged // i gets set to the index of the last array element i = limit-1; // continue looping until no exchanges are made while (i > 0) { // start lastExchangeIndex at 0 lastExchangeIndex = 0; // scan the sublist oarr from 0 to i for (j=0; j < i; j++) // exchange a pair if out of order if (oarr[j+1] < oarr[j]) { IntSwitch(oarr[j],oarr[j+1]); // record lastExchangeIndex lastExchangeIndex = j; } // set i to index of the last exchange // (array contents after i are already all sorted.) // continue sorting elements 0 to i in oarr i = lastExchangeIndex; } }
From the main program, we need an array declaration, for example,
const int ARRLIMIT = 20; int somearray[ARRLIMIT];The following code is typed according to the type of the array somearray which is declared to be int. The code needs appropriate modifications if the array is of some other type. We need both a "switching" procedure and a "sorting" procedure (or we could integrate the switch at the appropriate place in the body of the "sorting" code).
void IntSwitch(int & first, int & second) { int temp; temp = first; first = second; second = temp; } void IntSelectionSort (int oarr[], int limit) { // This subprogram sorts an integer array // via selectionsort. (version Ford & Topp 781-82) // oarr is the input array to be sorted // -- it is of type int // limit is the ACTUAL number of items // stored in oarr -- it may be // less than the declared size of oarr int i,j; // loop indices int smallIndex; // value of index of // smallest element // major loop -- scan sublist starting at i // until end of array for smallest element for (i = 0; i < limit-1; i++) { // i is the starting point for finding // the smallest element. We initially assume // that i is the smallest element and then update smallIndex = i; // now we look at elements between i and limit // to see if there is any smaller element // and reset smallIndex, if needed for (j = i+1; j < limit ; j++) if (oarr[j] < oarr[smallIndex]) smallIndex = j; // after completing scan for smallest element // exchange with head element in the sublist IntSwitch(oarr[i],oarr[smallIndex]); } }
This page is maintained by Dennis C. Smolarski, S.J.
© Copyright 1998, 1999 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 26 March 1999.