/* Chad Lupkes
   University of Phoenix
   POS 370
   Assignment 2
   Program Description: This program reads a list of numbers from a text file and
   performs three sorting algorithms.  The assignment only specified two, but the two copies
   of the assignment actually provided the basic algorithm for three, so I did all three
   as a test.  */

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

fstream data;

#define MAX2SORT 50
int data2sort[MAX2SORT], sorted_data[MAX2SORT]; // data2sort[] holds the unsorted array, 
                                                // sorted_data[] holds the sorted array.
int datacount = 0; // this is the count of numbers in the array
int temp; /* I used temp in two places.  
		     First was a holding variable for the data read from the disk file.
			 This allows me to test for the end of file marker before assigning that data
			 to the array.
			 The second place I used temp is as the storage location for the swap procedure. */
	


void main()
{
	int item; // item is the counter for the printing procedures

	data.open("pos370-asgn1.txt", std::ios::in); // Open the file
	while (! data.eof())  // Tests for the end of file marker
	{
		data >> temp;  // loads next datapoint from file into temp

//		if (!data.eof())  /* tests again for the end of file marker.  we only want to load
//							 the data into the array if it is not the end of file marker  */
//		{		
			data2sort[datacount] = temp; // loads the datapoint into data2sort array
			sorted_data[datacount] = temp;  // loads the datapoint into sorted_data array
			datacount++; // increment datacount
//		}
	}
	data.close(); // close the file

// Print initial data array, + 1 element

	cout << "Initial Data in arrays:" << endl;
	for (int count = 0; count < datacount; count++)
		cout << setw(3) << count << ": "<< setw(6) << data2sort[count] << ", " << setw(6) << sorted_data[count] << endl;
	
	cout << endl;  // Space between results

	/* Algorithm 1 - 	*/
	
	int y;
	int x;

	for (y = 1; y < datacount; y++) // Outside Counter
	{
		for (x = y; x > 0; x--) // Inside Counter
		{
			if (sorted_data[x-1]>=sorted_data[x])
			{
				temp = sorted_data[x-1];
				sorted_data[x-1] = sorted_data[x];
				sorted_data[x] = temp;
			}
		}
	}

	cout << "Results of Algorithm 1:" << endl;
	for (item=0; item < datacount; item++) // Print results from Algorithm1
		cout << setw(3) << item << ": " << setw(6) << data2sort[item] << ", " << setw(6) << sorted_data[item] << endl;
	
	cout << endl;  // Space between results

	for (item=0; item < datacount; item++)  // Restore sorted_data array to original numbers
		sorted_data[item] = data2sort[item];

/* Algorithm 2 - Bubble Sort */	
	
	for (y = datacount-1; y >= 2; y--)
	{
		for (x = 0; x < y; x++)
		{
			if (sorted_data[x] > sorted_data[x+1])
			{
				temp = sorted_data[x];
				sorted_data[x] = sorted_data[x+1];
				sorted_data[x+1] = temp;
			}
		}
	}
	
	cout << "Results of Algorithm 2:" << endl;
	for (item=0; item < datacount; item++) // Print results from Algorithm2
		cout << setw(3) << item << ": " << setw(6) << data2sort[item] << ", " << setw(6) << sorted_data[item] << endl;

	cout << endl;  // Space between results

	for (item=0; item < datacount; item++)  // Restore sorted_data array to original numbers
		sorted_data[item] = data2sort[item];

//	Algorithm 3, the flag bubble sort
	
	bool flag = 0;

    do
    {
		flag = 0;  // set flag to false
		for (x = 0; x < (datacount - 1); x++)
		{
			if (sorted_data[x] > sorted_data[x+1])
			{
				temp = sorted_data[x];
				sorted_data[x] = sorted_data[x+1];
				sorted_data[x+1] = temp;
				flag = 1;  // set flag to true
               }
          }
     } while ( flag != 0 );  // test if a swap has occured

	cout << "Results of Algorithm 3:" << endl;
	for (item=0; item < datacount; item++) // Print results from Algorithm3
		cout << setw(3) << item << ": " << setw(6) << data2sort[item] << ", " << setw(6) << sorted_data[item] << endl;


}