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

//  Algorithm: an improved bubble 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 bottom;             // "False bottom" for each pass
    int lastSwapIndx;       // Index of last element swapped
    int i;
    int temp;

    bottom = vSize - 1;
    while (bottom > 0) {
        lastSwapIndx = 0;
        for (i = 0; i < bottom; i++)
            if (vec[i] > vec[i+1]) {
                temp = vec[i];
                vec[i] = vec[i+1];
                vec[i+1] = temp; 
                lastSwapIndx = i;
            }
        bottom = lastSwapIndx;
    }
}


