Write a function template selectionSort based on Fig. 8.13. Write a driver program that inputs, sorts and outputs an int array and a float array.
What will be an ideal response?
```
// This program puts values into an array, sorts the values into
// ascending order, and prints the resulting array.
#include
#include
using namespace std;
// function template for insertionSort
template < typename T >
void insertionSort( T * const array, int size )
{
T insert; // temporary variable to hold element to insert
// loop over size elements
for ( int next = 0; next < size; next++ )
{
insert = array[ next ]; // store value in current element
int moveItem = next; // initialize location to place element
// search for place to put current element
while ( moveItem > 0 && array[ moveItem - 1 ] > insert )
{
// shift element right one slot
array[ moveItem ] = array[ moveItem - 1 ];
moveItem--;
} // end while
array[ moveItem ] = insert; // place inserted element
} // end for
} // end function template insertionSort
// function template for printArray
template < typename T >
void printArray( T * const array, int size )
{
for ( int i = 0; i < size; i++ )
cout << setw( 6 ) << array[ i ];
} // end function template printArray
int main()
{
const int SIZE = 10; // size of array
int a[ SIZE ] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
// display int array in original order
cout << "int data items in original order\n";
printArray( a, SIZE ); // print int array
insertionSort( a, SIZE ); // sort int array
// display int array in sorted order
cout << "\nint data items in ascending order\n";
printArray( a, SIZE );
cout << "\n\n";
// initialize double array
double b[ SIZE ] =
{ 10.1, 9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1 };
// display double array in original order
cout << "double point data items in original order\n";
printArray( b, SIZE ); // print double array
insertionSort( b, SIZE ); // sort double array
// display sorted double array
cout << "\ndouble point data items in ascending order\n";
printArray( b, SIZE );
cout << endl;
} // end main
```
int data items in original order
10 9 8 7 6 5 4 3 2 1
int data items in ascending order
1 2 3 4 5 6 7 8 9 10
double point data items in original order
10.1 9.9 8.8 7.7 6.6 5.5 4.4 3.3 2.2 1.1
double point data items in ascending order
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.1
You might also like to view...
The precision argument allows you to determine how many decimal places you want to round your numbers
Indicate whether the statement is true or false
In Publisher, after completing a mail merge, the new publication is still connected to the data source
Indicate whether the statement is true or false
There is a direct relationship between array names and ____________________data types.
Fill in the blank(s) with the appropriate word(s).
According to the Association of Chief Police Officers, which of the following is NOT a part of computer forensics?
a. collection of computer-related evidence b. publication of computer-related evidence c. preservation of computer-related evidence d. analysis of computer-related evidence