Im trying to write a program where i put an array into 4 different functions and time the sorts.To do that im supposed to write a copy function that copys the original array into a copied array that i sent into the function.
this is my current code:
i know the copy function is not completely right and my current problem is that nothing displays.any help is massively appreciated.Note:i am not the best programmer in the world,so be kind.Code:#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>
#include<string.h>
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);
int copy(int copiedarray[],int numbers[]);
//FUNCTIONS END
ofstream outf("f:Proj1-Output.dat", ios::out);
ifstream inf("f:Proj1-Input.dat", ios::in);
//-------------------MAIN-----------------------------------------------------------
int main()
{
float t1, t2, t3, t4;
long int start,end;
int array_cnt=0;
int results1;
int results2;
int results3;
int results4;
srand(time(0));
int arraysize = 10;
int numbers[arraysize];
int copiedarray[arraysize];
outf<<"SIZE "<<"QUICK "<<"HEAP "<<"BUBBLE "<<"INSERTION "<<endl;
for (int global=1; global<=20; global++)
{
for (int i = 0; i < arraysize; i++)
{
array_cnt++;
}
outf<<array_cnt<<" ";
copy(copiedarray,numbers);
start= clock();
results1=quickSort(copiedarray,arraysize);
end= clock();
outf<<results1;
outf<<(end-start)/CLOCKS_PER_SEC<<" ";
start= clock();
results2= insertion_sort(numbers,arraysize);
end= clock();
start= clock();
results3= BubbleSort(numbers,arraysize);
end= clock();
start= clock();
results4= heapSort(numbers,arraysize);
end= clock();
}
system("pause");
return 0;
}
//END OF MAIN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//BUBBBLE START
int BubbleSort(int a[], int array_size)
{
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;
}
}
}
}
//BUBBLE END
//INSERTION START
int insertion_sort(int a[], int array_size)
{
int i, j, key;
for(j = 1; j < array_size; j++)
{
key = a[j];
for(i = j - 1; (i >= 0) && (a[i] > key); i++)
{
a[i+1] = a[i];
}
a[i+1] = key;
}
}
//INSERTION END
//QUICK START
int quickSort(int a[], int array_size)
{
int i;
q_sort(a, 0, array_size - 1);
}
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)
{
int i;
int end;
heapify(a, array_size);
for (end = array_size - 1; end >= 1; end--)
{
swap(a[end], a[0]);
siftDown(a, 0, end - 1);
}
}
//HEAP END
int copy(int a[],int b[])
{
int arraysize;
for (int i = 0; i < arraysize; i++)
{
int a[i];
int b[i];
a[i]=b[i];
}
return 0;
}

