//----------------------------------------------------------------------
//  selsort.cpp
//  This program inputs up to MAX integer values from standard input.
//  The values are sorted into ascending order and output to standard
//  output.  The sorting method used is the straight selection sort.
//----------------------------------------------------------------------
#include <iostream.h>

const int MAX = 100;                   // Global constant

void InputVec( int[], int& );          // Function prototypes
void OutputVec( const int[], int );
void Sort( int[], int );
void Swap( int&, int& );

int main()
{
    int vec[MAX];       // Vector for storing input data
    int numItems;       // Number of data items entered by user

    cout << "This program sorts up to " << MAX << " input integers.\n"
         << "Use end-of-file to terminate the list early.\n\n"
         << "Begin input:\n";

    InputVec(vec, numItems);
    Sort(vec, numItems);
    // ASSERT: vec[0..numItems-1] are in ascending order

    cout << "\nSorted list:\n";
    OutputVec(vec, numItems);

    return 0;
}
//----------------------------------------------------------------------
void InputVec( /* out */ int  vec[],
               /* out */ int& size  )
    //..................................................................
    // POST: size == no. of input values
    //    && vec[0..size-1] contain the input values
    //..................................................................
{
    size = 0;
    while (size < MAX && cin >> vec[size])  // INV (prior to test):
                                             //    vec[0..size-1] have
                                             //       been input
                                             // && size <= MAX
        size++;
}
//----------------------------------------------------------------------
void OutputVec( /* in */ const int vec[],
                /* in */       int size  )
    //..................................................................
    // PRE:  Assigned(size)  &&  Assigned(vec[0..size-1])
    // POST: vec[0..size-1] have been output, one number per line
    //..................................................................
{
    int i;

    for (i = 0; i < size; i++)   // INV (prior to test):
                                 //     vec[0..i-1] have been output
                                 //  && i <= size
        cout << vec[i] << '\n';
}
//----------------------------------------------------------------------
void Sort( /* inout */ int vec[],
           /* in */    int size  )
    //..................................................................
    // PRE:  Assigned(size)  &&  Assigned(vec[0..size-1])
    // POST: vec[0..size-1] contain same values as vec[0..size-1]<entry>
    //       but are rearranged into ascending order
    //..................................................................
{
    int maxIndx;            // Index of largest no. in each pass
    int bottom;             // "False bottom" for each pass
    int i;

    for (bottom = size-1; bottom >= 1; bottom--) {
                    // INV (prior to test):
                    //     All vec[0..bottom] are <= vec[bottom+1]
                    //  && vec[bottom+1..size-1] are in ascending order
                    //  && bottom >= 0
        maxIndx = 0;
        for (i = 1; i <= bottom; i++)  // INV (prior to test):
                                       //     vec[maxIndx] >= all
                                       //        vec[0..i-1]
                                       //  && i <= bottom+1
            if (vec[i] > vec[maxIndx])
                maxIndx = i;

        if (maxIndx != bottom)
            Swap(vec[bottom], vec[maxIndx]);
    }
}
//----------------------------------------------------------------------
void Swap( /* inout */ int& int1,
           /* inout */ int& int2 )
    //..................................................................
    // PRE:  Assigned(int1)  &&  Assigned(int2)
    // POST: int1 == int2<entry>  &&  int2 == int1<entry>
    //..................................................................
{
    int temp = int1;

    int1 = int2;
    int2 = temp;
}


