Code:
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>
using std::rand;
using std::srand;
using namespace std;
//FUNCTIONS START***************************************************************
int BubbleSort(int numbers[],int arraysize);
int insertion_sort(int numbers[], int arraysize);
int quickSort(int numbers[], int array_size);
int q_sort(int a[], int left, int right);
int heapSort(int numbers[], int array_size);
int heapify(int numbers[],int array_size);
int siftDown(int numbers[], int start,int end);
void copy(int copiedarray[], int numbers[],int arraysize);
//FUNCTIONS END*****************************************************************
ofstream outf("f:Proj3-Output.dat", ios::out);
ifstream inf("f:Proj3-input.dat", ios::in);
//******************************************************************************
//**********************************MAIN****************************************
//******************************************************************************
int main()
{
float t1, t2, t3, t4;
int array_cnt;
int results1;
int results2;
int results3;
int results4;
int x_1, x_2;
srand(time(0));
int arraysize = 10000;
int numbers[arraysize];
int copiedarray[arraysize];
for (int i = 0; i < arraysize; i++)
numbers[i] = 1+ rand() % 100000;
outf<<"SIZE "<<"QUICK "<<"HEAP "<<"BUBBLE "<<"INSERTION "<<endl;
for (int global=1; global<=20; global++)
{
for (int i = 0; i < arraysize; i++)
{
arraysize=arraysize+1500;
array_cnt++;
}
copy(copiedarray,numbers,arraysize);
results1=BubbleSort(copiedarray,arraysize);
copy(copiedarray,numbers,arraysize);
results2=insertion_sort(copiedarray, arraysize);
copy(copiedarray,numbers,arraysize);
results3=quickSort(copiedarray, arraysize);
copy(copiedarray,numbers,arraysize);
results4=heapSort(copiedarray, arraysize);
}
return 0;
}
//******************************************************************************
//********************************** END OF MAIN********************************
//******************************************************************************
//******************************************************************************
//********************************BUBBBLE START*********************************
//******************************************************************************
int BubbleSort(int a[], int array_size)
{
long int start, end;
start= clock();
int i, j, temp;
for (i = 0; i < (array_size - 1); ++i)
{
for (j = 0; j < array_size - 1 - i; ++j )
{
if (a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
end= clock();
outf<<(end-start)/CLOCKS_PER_SEC<<" ";
for(i=0; i<array_size; i++)
cout<<setw(7)<<a[i];
}
//******************************************************************************
//********************************BUBBBLE END***********************************
//******************************************************************************
//******************************************************************************
//******************************INSERTION START*********************************
//******************************************************************************
int insertion_sort(int a[], int array_size)
{
long int start, end;
start= clock();
int i, j, key;
for(j = 1; j < array_size; j++) //Notice starting with 1 (not 0)
{
key = a[j];
for(i = j - 1; (i >= 0) && (a[i] > key); i++) //Move smaller values up one position
{
a[i+1] = a[i];
}
a[i+1] = key; //Insert key into proper position
}
end= clock();
outf<<(end-start)/CLOCKS_PER_SEC<<" ";
for(i=0; i<array_size; i++)
cout<<setw(7)<<a[i];
}
//******************************************************************************
//********************************INSERTION END*********************************
//******************************************************************************
//******************************************************************************
//**********************************QUICK START*********************************
//******************************************************************************
int quickSort(int a[], int array_size)
{
long int start, end;
start= clock();
int i;
q_sort(a, 0, array_size - 1);
end= clock();
outf<<(end-start)/CLOCKS_PER_SEC<<" ";
for(i=0; i<array_size; i++)
cout<<setw(7)<<a[i];
}
int q_sort(int a[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = a[left];
while (left < right)
{
while ((a[right] >= pivot) && (left < right))
right--;
if (left != right)
{
a[left] = a[right];
left++;
}
while ((a[left] <= pivot) && (left < right))
left++;
if (left != right)
{
a[right] = a[left];
right--;
}
}
a[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(a, left, pivot-1);
if (right > pivot)
q_sort(a, pivot+1, right);
}
//******************************************************************************
//********************************QUICK END*************************************
//******************************************************************************
//******************************************************************************
//***********************************HEAP START*********************************
//******************************************************************************
int heapify(int a[],int array_size)
{
int start= (array_size- 2) / 2;
for(; start >= 0; start--)
{
siftDown(a, start, array_size-1);
}
return 0;
}
int siftDown(int a[], int start,int end)
{
int root;
int child;
root=start;
while (root * 2 <= end)
{
if (root*2 == end)
child = root * 2;
else if(a[root * 2] > a[root * 2 + 1])
child = root * 2;
else
child = root * 2 + 1;
if(a[root] < a[child])
{
swap(a[root], a[child]);
root = child;
}
else
return 0;
}
}
int heapSort(int a[], int array_size)
{
long int start, end;
start= clock();
int i;
heapify(a, array_size);
for (end = array_size - 1; end >= 1; end--)
{
swap(a[end], a[0]);
siftDown(a, 0, end - 1);
}
end= clock();
outf<<(end-start)/CLOCKS_PER_SEC<<" "<<endl<<endl;
for(i=0; i<array_size; i++)
cout<<setw(7)<<a[i];
cout<<endl<<endl;
}
//******************************************************************************
//***********************************HEAP END***********************************
//******************************************************************************
//COPY START
void copy(int copiedarray[], int numbers[],int arraysize)
{
for (int i = 0; i < arraysize; i++)
{
copiedarray[arraysize]=numbers[arraysize];
}
}