//
// CS 202 demonstration program 7
// Written by Dave Williams for 9/25/03 class
//
// Program to sort a list of numbers from largest to smallest using the
// selection sort method.
//
const int MAXT = 10; // 10 is max number of elements - change if you wish
#include <iostream.h>
#include <stdlib.h>
void Sel_sort(int terms, float input[]);
int main()
{
float input[MAXT];
int j, terms;
cout << "\n\nProgram to sort a list of numbers from largest \n"
<< "to smallest value using the selection sort method.";
while(1)
{
cout << "\n\n\n\nHow many elements to sort (0 to quit)? ";
cin >> terms;
if (terms == 0)
exit(0);
if (terms > 10)
{
cout << "Too many - max number permitted is 10." << endl;
terms = 10;
}
cout << "\n\n" << "Input " << terms << " numbers to sort: ";
for (j = 0; j <= (terms-1); j++) // loop to read input data into array
cin >> input[j];
Sel_sort(terms, input);
}
return 0;
}
//
// function Sel_sort to actually perform the sort; swaps highest remaining
// element into each position starting from the top of the sorted array
//
void Sel_sort(int terms, float orig[])
{
float sorted[MAXT], temp;
int j, k;
for (j = 0; j <= (terms-1); j++) // assigns value of sorted[] starting
{ // with the first position element
sorted[j] = orig[j]; // start with the first element in orig[]
// just for the fun of it.
for(k = j; k <= (terms-1); k++)
{
if (orig[k] > sorted[j]) // if we find a bigger element than that,
{
temp = sorted[j];
sorted[j] = orig[k]; // swap them
orig[k] = temp;
}
}
} // and move to the next element in sorted[]
cout << "\n\nInput data sorted from highest to lowest: " << endl << endl;
for (j = 0; j <= (terms-1); j++)
cout << " [" << sorted[j] << "] ";
return;
}
// 
// 
//