//Xinnuo Wanng                CS1A                        Chapter 8 #10 Page.489
/*******************************************************************************
 * Sort Arrays
 * _____________________________________________________________________________
 * The program uses two identical arrays of just eight integers, displays the 
 * first and second arrays, sorts the first array using bubble sort, sorts 
 * the other using selection sort, and print out the array contents after each 
 * pass of the sort.
 * _____________________________________________________________________________
 * Input
 *    num    : The number
 * 
 * ****************************************************************************/
#include <iostream>
using namespace std;
void bubble(int arr[], int size);
void selection (int arr2[],int size);
void swap(int &a, int & b);
void print (int arr[],int size);
int main() 
{
	int arr[]={27, 11, 99, 888, 666, 999,776, 0 };
	int size = sizeof(arr)/sizeof(arr[0]);
	int arr2[size];
	for (int i = 0;i< size; i++)
	{
	     arr2[i]=  arr[i];
	     cout << arr[i]<<" ";
	}
	cout << endl;
	bubble(arr, size);
	print (arr,size);
	cout << endl;
	selection(arr2,size);
	print (arr2,size);
	cout << endl;
	return 0;
}
void bubble(int arr[], int size)
{
	for(int i = size-1;i > 0; i--)
	{
		for(int j = 0; j < i; j++)
		{
			if(arr[j]>arr[j+1])
			{
				swap(arr[j],arr[j+1]);
			}
		}
	}
}
void selection (int arr2[],int size)
{
	for (int start = 0; start < size; start++)
	{
		int minIndex = start;
		for (int i = start +1; i< size; i ++)
		{
			if (arr2[i]< arr2[minIndex])
			{
				minIndex =i;
			}
		}
		swap(arr2[minIndex], arr2[start] );
	}
}
void print (int arr[],int size)
{
	for(int i = 0; i< size; i++)
	{
		cout << arr[i]<<" ";
	}
}
void swap (int &a, int & b)
{
	int temp = a;
	a=b;
	b = temp;
}