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

    Insertion Sort

********************************************************************************************/
#include <iostream>
#include <string>

#include "sorttool.h"

using namespace std;
void InsertSort(int [], const int maxsize, const int used);

void main()
    {
    const int MaxSize = 1000;
    int Array[MaxSize];

    int toSort;
    cout << "How many numbers to sort (up to " << MaxSize << "): ";
    cin >> toSort;

    FillWithRand(Array, MaxSize, toSort); //setup the array with X numbers

    cout << "Unsorted:" << endl;
    ParseAndPrint(Array, MaxSize, toSort); //show the unsorted array

    InsertSort(Array, MaxSize, toSort); //sort the first X elements of the array

    cout << "Sorted:" << endl;
    ParseAndPrint(Array, MaxSize, toSort); //show the (hopefully) sorted array

    }

void InsertSort(int Array[], const int maxsize, const int count)
    {
    for (int i = 1; (i < count) && (i < maxsize); i++)
        if(Array[i] < Array[i-1])
            {
            int tooBig = Array[i];
            int hole;
            for (hole = i; (hole > 0) && (Array[hole-1] > tooBig); hole--)
                Array[hole] = Array[hole - 1];
            Array[hole] = tooBig;
            }

    return;
    }