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

//  Algorithm: straight selection sort

void Sort( int vec[], int vSize )
{
  int maxIndx; // Index of largest
  int bot;     // "False bot" for pass
  int i;
  
  // INV: bot >= 0 
  // && each vec[0..bot] is <= vec[bot+1]
  // && vec[bot+1..vSize-1] nondecreasing
  for (bot = vSize-1; bot >= 1; bot--) {
    maxIndx = 0;
    // INV: i <= bot+1
    // && vec[maxIndx] >= all vec[0..i-1]
    for (i = 1; i <= bot; i++) {
      if (vec[i] > vec[maxIndx]) {
	maxIndx = i;
      }
    }
    // ASSERT: vec[maxIndx]
    // is the maximum of vec[0..bot]
    swap(vec[bot], vec[maxIndx]);
  }
}

