#include #include #include using namespace std; void swap(int & x, int & y) { int t = x; x = y; y = t; } void ssort(int A[], int N) { int end = N - 1; for (int start = 0; start <= end; start += 1) { int smpos = start; for (int i = start+1; i <= end; i += 1) if (A[i] < A[smpos]) smpos = i; swap(A[smpos], A[start]); } } double get_cpu_time() { struct rusage ruse; getrusage(RUSAGE_SELF, &ruse); return ruse.ru_utime.tv_sec+ruse.ru_utime.tv_usec/1000000.0 + ruse.ru_stime.tv_sec+ruse.ru_stime.tv_usec/1000000.0; } int main(int argc, char * argv[]) { if (argc < 2) { cout << "Give me the size on the command line.\n"; exit(1); } int size = atoi(argv[1]); int * data = new int[size]; srandomdev(); for (int i = 0; i < size; i += 1) data[i] = random(); double t1 = get_cpu_time(); ssort(data, size); double t2 = get_cpu_time(); cout << "k = " << (t2-t1)/(size*size) << "\t"; cout << "It took " << t2-t1 << " seconds to sort.\n"; }