//DISPLAY 7.12 Sorting an Array //Tests the procedure sort. #include void fillArray(int a[], int size, int& numberUsed); //Precondition: size is the declared size of the array a. //Postcondition: numberUsed is the number of values stored in a. //a[0] through a[numberUsed - 1] have been filled with //nonnegative integers read from the keyboard. void sort(int a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <= ... <= a[numberUsed - 1]. void swapValues(int& v1, int& v2); //Interchanges the values of v1 and v2. int indexOfSmallest(const int a[], int startIndex, int numberUsed); //Precondition: 0 <= startIndex < numberUsed. Referenced array elements have //values. //Returns the index i such that a[i] is the smallest of the values //a[startIndex], a[startIndex + 1], ..., a[numberUsed - 1]. int main( ) { using namespace std; cout << "This program sorts numbers from lowest to highest.\n"; int sampleArray[10], numberUsed; fillArray(sampleArray, 10, numberUsed); sort(sampleArray, numberUsed); cout << "In sorted order the numbers are:\n"; for (int index = 0; index < numberUsed; index++) cout << sampleArray[index] << " "; cout << endl; return 0; } //Uses iostream: void fillArray(int a[], int size, int& numberUsed) void sort(int a[], int numberUsed) { int indexOfNextSmallest; for (int index = 0; index < numberUsed - 1; index++) {//Place the correct value in a[index]: indexOfNextSmallest = indexOfSmallest(a, index, numberUsed); swapValues(a[index], a[indexOfNextSmallest]); //a[0] <= a[1] <=...<= a[index] are the smallest of the original array //elements. The rest of the elements are in the remaining positions. } } void swapValues(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; } int indexOfSmallest(const int a[], int startIndex, int numberUsed) { int min = a[startIndex], indexOfMin = startIndex; for (int index = startIndex + 1; index < numberUsed; index++) if (a[index] < min) { min = a[index]; indexOfMin = index; //min is the smallest of a[startIndex] through a[index] } return indexOfMin; }