//----------------------------------------------------------------------
//  IMPLEMENTATION FILE (slctsort.cpp)
//  This module exports a function to sort an integer vector
//  into ascending order.
//----------------------------------------------------------------------
#include "slctsort.h"

//  Algorithm: straight selection sort

void Sort( /* inout */ int vec[],
           /* in */    int vSize )
    //..................................................................
    // PRE:  Assigned(vSize)  &&  Assigned(vec[0..vSize-1])
    // POST: vec[0..vSize-1] contain same values as
    //       vec[0..vSize-1]<entry> but are sorted into ascending order
    //..................................................................
{
    int maxIndx;            // Index of largest no. in each pass
    int bottom;             // "False bottom" for each pass
    int i;
    int temp;

    for (bottom = vSize-1; bottom >= 1; bottom--) {
                    // INV (prior to test):
                    //      All vec[0..bottom] are <= vec[bottom+1]
                    //   && vec[bottom+1..vSize-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;

        temp = vec[bottom];
        vec[bottom] = vec[maxIndx];
        vec[maxIndx] = temp; 
    }
}


