lundi 1 avril 2019

Bubblesort on List - too much time spent on computing

I've created a code responsible for doing bubblesort on list. It seems to work but takes a loot of time to execute . I would be glad if someone would tell me what's wrong in it , so I can avoid doing same mistake in the future

Thought that it may be something connected with auto , but rewriting code did nothing.

void Sorting::bubblesort(std::list<int>::iterator start, std::list<int>::iterator stop)
{
    int k = 0;
    int temp;
    std::list<int>::iterator j_1 ;
    for (auto i = start; i != stop; i++)
    {
        for (auto j = std::next(start, 1); j != std::prev(stop, k); j++)
        {
            j_1= std::prev(j, 1);
            if (*j_1 > *j)
            {
                temp = *j_1;
                *j_1 = *j;
                *j = temp;
            }
        }
        k++;
    }
}

Tested on 1000 elements - 9,032 s (measured with std::chrono)

Aucun commentaire:

Enregistrer un commentaire