Performance time for quickort

hi i need to write a program that sort a vector of random numbers with quicksort and need to know the time in seconds. i know i can do something like


clock_t begin=clock();



and then


clock_t end=clock();
clock_t clock = end - start;
double timeInSeconds = clock / (double) CLOCKS_PER_SEC;


however i can't put it in the function because it recursive so it will output me multiple times in seconds, can anyone suggest a solution or should i make a function that does this and in main put my sort function in between begin=clock and end=clock



thanks


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
quickSort(T arr[], int left, int right) {
    
    int i = left, j = right;
    int tmp;
    int pivot = arr[(left + right) / 2];
    
 
    while (i <= j) {
        while (arr[i] < pivot)
            i++;
        while (arr[j] > pivot)
            j--;
        if (i <= j) {
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
        
    }
    //recursion
    if (left < j)
        quickSort(arr, left, j);
    if (i < right)
        quickSort(arr, i, right);
Create a function that will do the timing for you:

1
2
3
4
5
6
7
8
9
10
11
template <typename T>
void quickSort(T arr[], int size) {
    std::cout << "----------------Sorting begins----------------\n";
    
    clock_t start = clock();    
    quickSort<int> (arr, 0, size - 1);
    start = clock() - start;
    
    std::cout << "Computation took " << ((double)start / CLOCKS_PER_SEC) << " seconds\n";
    std::cout << "-----------------Sorting ends-----------------\n";
}


http://coliru.stacked-crooked.com/a/6ef13efc2762a915
Topic archived. No new replies allowed.