//Tests the procedure bubsort. #include const int TRUE=1, FALSE=0; void fill_array(int a[], int size, int& number_used); //Precondition: size is the declared size of the array a. //Postcondition: number_used is the number of values stored in a. //a[0] through a[number_used - 1] have been filled with //nonnegative integers read from the keyboard. void bubsort(int a[], int number_used); //Precondition: number_used <= declared size of the array a. //The array elements a[0] through a[number_used - 1] have values. //Postcondition: The values of a[0] through a[number_used - 1] have //been rearranged so that a[0] <= a[1] <= ... <= a[number_used - 1]. //The algorithm is `bubble sort'. void swap_values(int& v1, int& v2); //Interchanges the values of v1 and v2. int main( ) { int sample_array[10], number_used; cout << "This program sorts numbers from lowest to highest.\n"; fill_array(sample_array, 10, number_used); bubsort(sample_array, number_used); cout << "In sorted order the numbers are:\n"; for (int index = 0; index < number_used; index++) cout << sample_array[index] << " "; cout << endl; return 0; } //Uses iostream.h: void fill_array(int a[], int size, int& number_used) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } number_used = index; } void bubsort(int a[], int number_used) { int times = 0, exchange=TRUE, current; while ((times < number_used - 1)&&(exchange)) { exchange = FALSE; for (current = 0; current < number_used-1-times; current++) {if (a[current] > a[current+1]) {swap_values(a[current], a[current+1]); exchange = TRUE;};}; times++; } } void swap_values(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; }