lundi 5 février 2018

How do you implement bubble sort on a vector?

for (i = 0; i < n; ++i)
{
    for (j = 0; j < n-i-1; ++j)
    {
        // Comparing consecutive data and switching values if value at j > j+1.
        if (arr[j] > arr[j+1])
        {
            arr[j] = arr[j]+arr[j+1];
            arr[j+1] = arr[j]-arr[j + 1];
            arr[j] = arr[j]-arr[j + 1];
        }
    }
    // Value at n-i-1 will be maximum of all the values below this index.
}   

I have this but it throws errors and I think it is for a string, not a vector and the things that I saw with vectors had something called "Play" I couldn't find out what that was.

Aucun commentaire:

Enregistrer un commentaire