/*
Authors:Travis Gadberry
	Patrick Hesser
	Chris Ladewig
Date:	9 Feb 05
Updated:16 Feb 05
File:	InsertionSort.cpp
*/


#ifndef INSERTIONSORT_H
#define INSERTIONSORT_H
  #include "InsertionSort.h"
#endif

InsertionSort::
InsertionSort() {
  n = 1;
  A = new int[n];
  A[0] = 0;
  sorted = true;
}

InsertionSort::
InsertionSort(int * newArray, int N) {
  n = N;
  A = new int[n];
  for(int i=0; i<n; i++)
    A[i] = newArray[i];
  sorted = false;
  resetIncrements();
}

InsertionSort::
~InsertionSort() {
  delete(A);
}

int *
InsertionSort::
getArray() {
  return A;
}

int
InsertionSort::
getN() {
  return n;
}

bool
InsertionSort::
getSorted() {
  return sorted;
}

double
InsertionSort::
getTime() {
  return time;
}

int
InsertionSort::
getIncrements() {
  return increments;
}

void
InsertionSort::
setArray(int * newArray) {
  delete(A);
  n = sizeof(newArray);
  A = new int[n];
  sorted = false;
}

void
InsertionSort::
sort() {
  clock_t t1, t2;
  t1 = clock();

  int k, j, key;
  for(k=1; k<n; k++) {
    key = A[k];
    j = k - 1;
    while(increments++, j >= 0 && A[j] > key) {
      A[j+1] = A[j];
      j--;
    }
    A[j+1] = key;
  }

  t2 = clock();
  time = (t2 - t1)/CLOCKS_PER_SEC;
}

void
InsertionSort::
setN(int newN) {
  n = newN;
}

void
InsertionSort::
setSorted(bool newSorted) {
  sorted = newSorted;
}

void
InsertionSort::
resetTime() {
  time = 0.0;
}

void
InsertionSort::
resetIncrements() {
  increments = 0;
}

