I have googled this and I see how to do the clock function with insertion and I was pretty sure I implemented it right but the insertion sort never shows any time. I am not sure if I am not waiting long enough but I have had the program up for atleast 20 minutes and stil nothing has happened. I do not know if it is my computer in anyway. I just wanted to see if anyone saw an error that I am overlooking.
#include <iostream>
#include <fstream>
#include <ctime>
#include <math.h>
using namespace std;
//array size
const int CAPACITY = 500000;
void ReadFile(int*);
void InsertionSort(int*);
void CombSort(int*, size_t size);
int main()
{
//Variables
int* info = NULL;
info = new int[CAPACITY];
int* temp = NULL;
temp = new int[CAPACITY];
int t = 0;
int z;
int k;
clock_t end;
clock_t start;
double times = 0;
for(z = 0; z < 1; z++)
{
ReadFile(info);
start = clock();
cout << "Insertion Sort: ";
InsertionSort(int* info);
end = clock();
times = ((end-start)/(double)CLOCKS_PER_SEC)
}
cout << times << endl;
times=0;
}
for(z = 0; z < 1; z++)
{
ReadFile(info);
start = clock();
CombSort(info, (size_t)CAPACITY);
end = clock():
times = ((end-start)/(double)CLOCKS_PER_SEC)
}
cout << "Comb Sort: ";
cout << times << endl;
times = 0;
break;
}
}
return 0;
}
void ReadFile(int* info)
{
//Read in the file
fstream datafile("numbers.txt", ios::in |ios::out);
//Test for file open
if(!datafile.is_open())
{
cout<<"Error opening file"<<endl;
system("pause");
exit(-1);
}
//places data from the file into string array
for(int i = 0; !datafile.eof(); i++)
{
datafile >> info[i];
}
}
void InsertionSort(int* info)
{
//variables
int t, z;
int temp;
//cycles through the array
for (t = 1; t < CAPACITY; t++)
{
//sets j to current element in the array, i.
z = t;
//cycles the element down as long as the previous
//element is bigger
while( (z > 0) && (info[z] < info[z - 1] ))
{
//stores current element
temp = info[z];
//places previous element into current element spot
info[z] = info[z - 1];
//places previous element as temp (the old current elemnt)
info[z - 1] = temp;
//moves z down
z--;
}
}
}
void CombSort(int* info, size_t size)
{
//variables
size_t Gap = size;
bool swap_b = false;
while ((Gap > 1) || swap_b)
{
//calculates the gaps used for the sort
if (Gap > 1)
{
Gap = (size_t)((double)Gap / 1.5); //
}
//keeps cycles from ending prematurely
swap_bool = false;
for (size_t t = 0; Gap + t < size; t++)
{
//determiens if the current element is bigger than the
//element located by the gap
if (info[t] - info[t + Gap] > 0)
{
//if so, it swaps the elements and sets swap_b
//to true
int swapval = info[t];
info[t] = info[t + Gap];
info[t + Gap] = swapval;
swap_bl = true;
}
}
}
}