3

Problem was in IDE I am using - CLion 1.2.4 gives incorrect output inside its own output window, solved.

Following code gives repeating output when working with vectors, bigger then ~1000, like:

bubblebubble0.265596bubble0.2655960.171889bubble0.2655960.1718890.265644 shell000

If I call endl after every output, all seems fine:

bubble
0
0.015626
0.015628
shell
0
0
0

But when I try to clear buffer with .flush(), I'm getting same repeating output.

#include <iostream>
#include <vector>
#include <chrono>
#include <algorithm>

using namespace std;

vector<int> type_sort(string type, vector<int>input) {
    int n = input.size();

    if (type == "bubble") {
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n - 1; j++)
                if(input[j] > input[j+1]) {
                    swap(input[j], input[j+1]);
                }
    }
    else if (type == "shell") {
        int gap, i, j, temp;
        for (gap = n/2; gap > 0; gap /= 2)
            for (i = gap; i < n; i++)
                for (j = i - gap; j >= 0 && input[j] > input[j + gap]; j -= gap) {
                    temp = input[j];
                    input[j] = input[j + gap];
                    input[j + gap] = temp;
                }
    }
    return input;
}

void print_vector(vector<int> input) {
    for(int i = 0; i < input.size(); i+= input.size() / 5) {
        cout << input[i] << ",..";
    }
    cout << endl;
}

vector<int> random_vector(int size) {
    vector<int> output;
    for(int i = 0; i < size; i++) {
        output.push_back(rand() % 20 + 1);
    }
    return output;
}

int main() {
    vector<int> numbers = random_vector(5000),
                sorted_numbers,
                reversed_numbers;
    sorted_numbers = reversed_numbers = numbers;

    sort(sorted_numbers.begin(), sorted_numbers.end());
    sort(reversed_numbers.begin(), reversed_numbers.end(), greater<int>());

    vector<vector<int>> sort_types = {numbers, sorted_numbers, reversed_numbers};
    vector<string> sort_names = {"bubble", "shell"};

    chrono::time_point<chrono::system_clock> start_time, end_time;

    for (int i = 0; i < 2; i++) {
        cout << sort_names[i];
        for (int j = 0; j < 3; j++) {
            start_time = chrono::system_clock::now();
            type_sort(sort_names[i], sort_types[j]);
            end_time = chrono::system_clock::now();

            chrono::duration<double> elapsed_seconds = end_time - start_time;
            cout << elapsed_seconds.count();
        }
        cout << endl;
    }
    return 0;
}
tdwibar
  • 111
  • 1
  • 5
  • where did you use `flush()` in the code you posted? – Biruk Abebe Apr 25 '16 at 06:11
  • *"bigger then ~1000"* - that the output depends in such a way on the vector size hints at some memory corruption - I assume if you comment out the call to `type_sort` it works fine? If so, try changing the `vector` accesses from `[x]` to `.at(x)`, and add a `try`/`catch (const std::exception& e) { std::cerr << "caught " << e.what() << '\n'; }` block in `main()`. – Tony Delroy Apr 25 '16 at 07:12
  • bkVnet, I've even tried to use flush() in every iteration of inner cycle, still not working. Tony, thanks for reply, I'll try it out! – tdwibar Apr 25 '16 at 07:32
  • 1
    Still gives repeating output and catches no errors (I've replaced access method to .at() and added try/catch block inside cycle). Btw, you are right, problem is inside sort_type function. – tdwibar Apr 25 '16 at 10:27

1 Answers1

1

The problem was in IDE I am using - CLion 1.2.4 gives incorrect output.

enter image description here

However when I ran .exe directly inside explorer (cmd) error has gone.

enter image description here

tdwibar
  • 111
  • 1
  • 5
  • 1
    Wow - that's pretty crazy. Good find! Probably worth adding an update atop your question that the problem was caused by that IDE, so other people know immediately whether this Q&A may help them. – Tony Delroy Apr 25 '16 at 11:14