trouble using cout within recursive iter

hello,

im probably overthinking this, but im having trouble printing out the count of comparison that happens in quick_sort and merge_sort as well as properly calculating the execution time. (it prints out comparison and exec time each time it calls itself which is to be expected)

my current work around is making count global and printing comparison count and execution time calculations inside main

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
31
32
33
34
35
36
37
38
39
40
41
template <class Iterator>
inline void quick_sort(Iterator begin, Iterator end) {
    int count;
    // auto start = high_resolution_clock::now();
    if (end <= begin) {
        // auto stop = high_resolution_clock::now();
        // auto duration = duration_cast<nanoseconds>(stop - start);
        // cout << "  Comparisons: " << count << endl;
        // cout << "  Execution time: " << duration.count() << " nanoseconds" << endl;
        return;
    }
    Iterator pivot = begin, middle = begin + 1;
    for (Iterator i = begin + 1; i < end; ++i) {
        if (*i < *pivot) {
            std::iter_swap(i, middle);
            ++middle;
            count++;
        }
    }
    std::iter_swap(begin, middle - 1);
    quick_sort(begin, middle - 1);
    quick_sort(middle, end);
}

template <class Iterator>
inline void merge_sort(Iterator begin, Iterator end) {
    int count;
    // auto start = high_resolution_clock::now();
    if (end <= begin + 1) {
        // auto stop = high_resolution_clock::now();
        // auto duration = duration_cast<nanoseconds>(stop - start);
        // cout << "  Comparisons: " << count << endl;
        // cout << "  Execution time: " << duration.count() << " nanoseconds" << endl;
        return;
    }
    Iterator middle = begin + (end - begin) / 2;
    merge_sort(begin, middle);
    merge_sort(middle, end);
    std::inplace_merge(begin, middle, end);
    count++;
}


this is what inside main looks like
as i mentioned above, i made COUNT global. i also did the calculation inside main.

1
2
3
4
5
6
7
8
9
cout << "////////////////////////////////" << endl;
    cout << "QUICK sort: " << endl; COUNT = 0;
    auto start = high_resolution_clock::now();
    quick_sort(list1.begin(), list2.end());
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<nanoseconds>(stop - start);
    cout << "  Comparisons: " << COUNT << endl;
    cout << "  Execution time: " << duration.count() << " nanoseconds" << endl;
    display_(list1);
Last edited on
You must initialize count to zero.

Then have both functions return the number of comparisons:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
template <class Iterator>
inline int quick_sort(Iterator begin, Iterator end) {
    int count {};
    // auto start = high_resolution_clock::now();
    if (end <= begin) {
        // auto stop = high_resolution_clock::now();
        // auto duration = duration_cast<nanoseconds>(stop - start);
        // cout << "  Comparisons: " << count << endl;
        // cout << "  Execution time: " << duration.count() << " nanoseconds" << endl;
        return count;
    }
    Iterator pivot = begin, middle = begin + 1;
    for (Iterator i = begin + 1; i < end; ++i) {
        if (*i < *pivot) {
            std::iter_swap(i, middle);
            ++middle;
            count++;
        }
    }
    std::iter_swap(begin, middle - 1);
    count += quick_sort(begin, middle - 1);
    count += quick_sort(middle, end);
    return count;
}

template <class Iterator>
inline int merge_sort(Iterator begin, Iterator end) {
    int count {};
    // auto start = high_resolution_clock::now();
    if (end <= begin + 1) {
        // auto stop = high_resolution_clock::now();
        // auto duration = duration_cast<nanoseconds>(stop - start);
        // cout << "  Comparisons: " << count << endl;
        // cout << "  Execution time: " << duration.count() << " nanoseconds" << endl;
        return count;
    }
    Iterator middle = begin + (end - begin) / 2;
    count += merge_sort(begin, middle);
    count += merge_sort(middle, end);
    std::inplace_merge(begin, middle, end);
    return count + 1;
}


cout << "////////////////////////////////" << endl;
    cout << "QUICK sort: " << endl;
    auto start = high_resolution_clock::now();
    COUNT = quick_sort(list1.begin(), list2.end());
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<nanoseconds>(stop - start);
    cout << "  Comparisons: " << COUNT << endl;
    cout << "  Execution time: " << duration.count() << " nanoseconds" << endl;
    display_(list1);
Topic archived. No new replies allowed.