/*

    This is sorting code that uses operations of the listClass
    to do the sorting.

    Below are three functions: SortStructs, findMin, and swap.
    findMin and swap are "helper functions" that are used by
    SortStructs.

    SortStructs will sort the elements of an inventory into
    increasing order, by serial number.

    SortStructs inputs a parameter of type listClass.
    ("listClass" is defined in ListC.h and implemented in
    ListC.cpp).  The name of SortStructs' parameter is "inv".
    The parameter "inv" contains an array of structures.  Each
    structure represents one item of a store inventory.
    SortStructs rearranges the order of the structures in the
    array.  Each structure has several fields.  One of the
    fields is a serial number.  SortStructs puts the structures
    in order by increasing serial number.

*/

int findMin (listClass& inv, int firstPos)
{
  int posOfMin = firstPos , curPos ;
  inventoryItemType curItem, minItem ;
  bool Success ;

  for (curPos=firstPos; curPos <= inv.ListLength(); curPos++) 
  {
    inv.ListRetrieve(curPos, curItem, Success) ;
    inv.ListRetrieve(posOfMin, minItem, Success) ;
    if (curItem.serialNum < minItem.serialNum )
       posOfMin = curPos ;
  }
  return (posOfMin) ;
}

void swap (listClass& inv, int pos1, int pos2)
{
   inventoryItemType item1, item2 ;
   bool Success ;
   
   inv.ListRetrieve(pos1, item1, Success) ;
   inv.ListRetrieve(pos2, item2, Success) ;
   inv.ListDelete(pos1, Success);
   inv.ListInsert(pos1, item2, Success);
   inv.ListDelete(pos2, Success);
   inv.ListInsert(pos2, item1, Success);
}

void SortStructs (listClass& inv)
{
   int subListStart = 1, positionOfMin ;
   
   for (subListStart=1;
        subListStart < inv.ListLength();
        subListStart++)
   {
      positionOfMin = findMin(inv, subListStart) ;
      swap(inv, subListStart, positionOfMin) ;
   }
}