(Bubble Sort) In the bubble sort algorithm, smaller values gradually “bubble” their way up- ward to the top of the array like air bubbles rising in water, while the larger values sink to the bot- tom. The bubble sort makes several passes through the array. On each pass, successive pairs of elements are compared. If a pair is in increasing order (or the values are identical), we leave the values as they are. If a pair is in decreasing order, their values are swapped in the array. Write a program that sorts an array of 10 integers using bubble sort.

What will be an ideal response?

```
// This program sorts an array's values into ascending order.
#include
#include
using namespace std;

int main()
{
const int arraySize = 10; // size of array a
int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
int hold; // temporary location used to swap array elements

cout << "Data items in original order\n";

// output original array
for ( int i = 0; i < arraySize; i++ )
cout << setw( 4 ) << a[ i ];

// bubble sort
// loop to control number of passes
for ( int pass = 0; pass < arraySize - 1; pass++ )
{
// loop to control number of comparisons per pass
for ( int j = 0; j < arraySize - 1; j++ )
{
// compare side-by-side elements and swap them if
// first element is greater than second element
if ( a[ j ] > a[ j + 1 ] )
{
hold = a[ j ];
a[ j ] = a[ j + 1 ];
a[ j + 1 ] = hold;
} // end if
} // end for
} // end for

cout << "\nData items in ascending order\n";

// output sorted array
for ( int k = 0; k < arraySize; k++ )
cout << setw( 4 ) << a[ k ];

cout << endl;
} // end main
```

Computer Science & Information Technology

You might also like to view...

The ________ provides access to common functions via drop-down lists

A) Standard Toolbar B) Formatting Toolbar C) Menu Bar D) Title Bar

Computer Science & Information Technology

When updating an existing table of figures, you can choose to only update the page numbers or to update the entire ________

Fill in the blank(s) with correct word

Computer Science & Information Technology