I need to optimize this algo to not iterate over elements already sorted: void…
- User
I need to optimize this algo to not iterate over elements already sorted:
void bubble(RandomIter begin, RandomIter end, Comparator comp = Comparator{}) { // Random access iterators have the same traits you defined in the Vector class // For instance, difference_type represents an iterator difference // You may delete the types you don't use to remove the compiler warnings using _it = std::iterator_traits; using difference_type = typename _it::difference_type; using value_type = typename _it::value_type; using reference = typename _it::reference; using pointer = typename _it::pointer; std::cout << "start" << std::endl; std::cout << std::distance(begin, end) << std::endl;
if (std::distance(begin, end) == 0) { return; } for (RandomIter i = begin; i < end - 1; ++i) { for (RandomIter current = begin; current != end - 1; ++current) { RandomIter next = current + 1; if (comp(*next, *current)) { std::swap(*next, *current); } } /* for (RandomIter k = begin; k < end-1; ++k) { std::cout << *k << " "; } std::cout << std::endl; */ }}
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.