1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
#include <iomanip>
using namespace std;
//**********************************************************************
// Definition of function arrSelectSort.
// This function performs an ascending order selection sort on
// array, which is an array of pointers. Each element of array
// points to an element of a second array. After the sort,
// array will point to the elements of the second array in
// ascending order.
//**********************************************************************
void arrSelectSort(int *array[ ], int size)
{
int startScan, minIndex;
int *minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if ( *(array[index] ) < *minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArray(int array[ ], int size)
{
cout << "The Donations, in there original order are: " << endl;
for ( int i = 0; i < size; i++)
cout << setw( 4 ) << array[ i ];
}
void showArrPtr(int *array[ ], int size)
{
cout << "The Donations, in there ascending order are: " << endl;
for ( int j = 0; j < size; j++)
cout << setw( 4 ) << array[ j ];
}
int main()
{
const int NUM_SIZE = 15;
int donations[ NUM_SIZE ] = { 5, 100, 5, 25, 10, 5, 25, 5, 5, 100, 10, 15, 10, 5, 10 };
int *donationsPtr = donations;
arrSelectSort(donationsPtr, NUM_SIZE);
showArrPtr(donationsPtr, NUM_SIZE);
cout << " " << endl;
showArray(donations, NUM_SIZE);
cout << " " << endl;
system ("PAUSE");
return EXIT_SUCCESS;
}
|