#include <iostream>
#include <iomanip>
#include <algorithm>
#include <ctime>

using namespace std;

class StopWatch {
public:
	StopWatch();
	void start();
	void stop();
	void reset();
private:
	bool running;
	clock_t start_time;
	clock_t total_time;
friend ostream& operator<<(ostream& o, const StopWatch& sw);
};

StopWatch::StopWatch(): running(false), total_time(0) {
}

void StopWatch::start() {
	if (!running) {
		running=true;
		start_time=clock();
	}
}

void StopWatch::stop() {
	if (running) {
		running=false;
		total_time+=clock()-start_time;
	}
}

void StopWatch::reset() {
	total_time=0;
	if (running) {
		start_time=clock();
	}
}

ostream& operator<<(ostream& o, const StopWatch& sw) {
	clock_t res(sw.running?clock()-sw.start_time:sw.total_time);
	return o<<fixed<<setprecision(2)<<static_cast<double>(res)/CLK_TCK<<" sec";
}

void bubble_sort(int* begin, int* end) {
	for (int* p(end-1); p!=begin; --p) {
		for (int* q(begin); q!=p; ++q) {
			if (*q>*(q+1)) {
				int t(*q);
				*q=*(q+1);
				*(q+1)=t;
			}
		}
	}
}

int main() {
	srand(time(0));
	while (true) {
		cout<<"Geef N (of Ctrl+C om te stoppen): ";
		int n;
		cin>>n;
		if (n<2) {
            n=2;
        }
		int* array=new int[n];
		for (int i(0); i<n; ++i) {
			array[i]=rand();
		}
		StopWatch sw;
		sw.start();
		bubble_sort(array, array+n);
//		sort(array, array+n);
		sw.stop();
		delete[] array;
		cout<<"Tijdsduur: "<<sw<<endl;;
	}
}
