Help timing a sorting function

Can someone help or point me in the right direction? I'm writing a program that will implement BubbleSort and MergeSort and time them as they sort a dynamic array with N elements.

These are the instructions on what my main.cpp file should do.

main.cpp
Include all needed libraries for timing and random number generation
while the number of element, N, is less than 100001 repeat the following
create an array with N (initially 10) random elements
sort the same array with Bubble and Merge sort
time how long it takes each algorithm in microseconds (see example below)
Increase N by a factor of 10 (N *= 10)
Hint: you may want to put your merge sort object on the heap and delete it with every iteration of this loop

and this is what I have so far for my main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <sys/time.h>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <iomanip>
#include "BubbleSort.h"
//#include "MergeSort.h"

int main()
{
	BubbleSort<int> sort;
	//MergeSort<int> sort;
	
    int n=10;
    std::cout<<n;
    while(n < 10001)
    {
    	std::clock_t start;
    	start = std::clock();
        int* tempArr = new int[n];
        sort.sort(tempArr, n);
        //std::cout<<n;
        //MergeSort::sort(n);
        //std::cout<<sortedArr;
        std::cout<<tempArr;
        std::cout<<"this should be the time after each iteration"<<(std::clock()- start)/(double)(CLOCKS_PER_SEC/1000);
        n *= 10;
    }
}


One of the errors that I'm running into is that I'm not sure how to correctly call the function I think? Any help will be greatly appreciated.
Get the time
http://www.cplusplus.com/reference/chrono/high_resolution_clock/now/

Cast to whatever unit you want (like microseconds)
http://www.cplusplus.com/reference/chrono/duration_cast/

Hope this helps.
Unfortunately we are not allowed to use any library outside of what it's already included in the main file. Thanks though
You're not allowed to use the standard C++ stuff?

std::cout << "Sorted in " << ((double)(std::clock() - start) / CLOCKS_PER_SEC) << " seconds.\n";

Remember, CLOCKS_PER_SEC/1000 is an integer, chopped to something useless. ((double)CLOCKS_PER_SEC)/1000 is not.

Topic archived. No new replies allowed.