/********************************************************************************************
ezwInsrt.cpp
	Jim Millard
	for CO311

    EZwindows sort visualization for Insertion Sort

********************************************************************************************/

#include "ezwInsrt.h"
#include "ezwUtils.h"
#include "ezwMove.h"

extern float baseline; //Bottom edge of all bars
extern float tempX; //horizontal position used when swapped to a "temp" location

void InsertionSort(SimpleWindow& W, RaySegment* ptrR[], int A[], const int size, const short load)
    {
    RaySegment temp(W, tempX,baseline/2, tempX,baseline/2, White, ptrR[0]->GetThickness());

    for (int i = 1; i < size; i++)
        {
        if(A[i] < A[i-1])
            {
            //save off the out-of-place element to the temp area
            int tooBig = A[i];

            //highlight the one to move
            Highlight(*ptrR[i],Blue,load);
            
            //move it
            Move(*ptrR[i], temp, load);
    
            int hole;
            for (hole = i; (hole > 0) && (A[hole-1] > tooBig); hole--)
                {
                Move(A[hole-1], A[hole]);

                //highlight the one to move
                Highlight(*ptrR[hole-1],Magenta,load);
                
                //move it
                Move(*ptrR[hole-1], *ptrR[hole], load);
                //unhighlight it
                Highlight(*ptrR[hole],Red);
                }

            A[hole] = tooBig;
            Move(temp, *ptrR[hole], load);

            //unhighlight it
            Highlight(*ptrR[hole],Red);
            }
        }
    return;
    }