[ Back to the overview Matrix ]

Test case : Bubble sort using C++

Lines used: 22
#include <iostream>
#include <vector>

int main(void)
{
    std::vector<int> vec;

    for (int count = 0, tmp; count < 25; count++) {
        std::cout << '>' << std::flush;
        std::cin >> tmp;
        if (tmp == 0)
          break;
        vec.push_back(tmp);
    }

    bool swapped(false);
    do {
        swapped = false;
        for (unsigned i(0); i < vec.size() - 1; ++i)
            if (vec[i] > vec[i+1])
              std::swap(vec[i], vec[i+1]), swapped = true;
    } while (swapped);

    for (unsigned i(0); i < vec.size(); ++i)
      std::cout << vec[i] << std::endl;
}
Contributed by Jeremy Yallop , jeremy at yallop.org