Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ vector bubble sort

I use g++ -std=c++11 Sort.cpp to compile my file.

My problem is the bubble sort don't sort.

Maybe I'm passing the vector by value but I don't know is closely my firt time trying work with c++ and I chose use vector library.

My code is:

#include <iostream>
#include <vector>

using namespace std;

void bubbleSort(vector<int> a);

void printVector(vector<int> a);

int main(int argc, char const *argv[])
{
    vector<int> a{3,2,6,1};
    printVector(a);

    bubbleSort(a);
    printVector(a);
}

void bubbleSort(vector<int> a)
{
    bool swapp = true;
    while(swapp)
    {
        swapp = false;
        for (int i = 0; i < a.size()-1; i++)
        {
            if (a[i]>a[i+1] )
            {
                a[i] += a[i+1];
                a[i+1] = a[i] - a[i+1];
                a[i] -=a[i+1];
                swapp = true;
            }
        }
    }
}

void printVector(vector<int> a)
{
    for (int i=0;  i <a.size();  i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
}

In the main I declare a vector type of int's and make the list {3,2,6,1}

After that e call the function printVector wich pretends print all numbers of vector on console and call bubbleSort function and finally print again.

like image 350
warwcat Avatar asked Oct 25 '25 09:10

warwcat


2 Answers

You need to pass by reference; by making a copy, you sort a temporary copy, and then that's it; it disappears and the original is still unsorted, because, once again, you sorted only a temporary copy of the entire vector.

So change vector<int> a to vector<int>& a.

Here's the code, fixed:

http://coliru.stacked-crooked.com/a/2f118555f585ccd5

#include <iostream>
#include <vector>

using namespace std;

void bubbleSort(vector<int>& a);

void printVector(vector<int> a);

int main(int argc, char const *argv[])
{
 vector<int> a {3,2,6,1};

 printVector(a);

 bubbleSort(a);

 printVector(a);



}

void bubbleSort(vector<int>& a)
{
      bool swapp = true;
      while(swapp){
        swapp = false;
        for (size_t i = 0; i < a.size()-1; i++) {
            if (a[i]>a[i+1] ){
                a[i] += a[i+1];
                a[i+1] = a[i] - a[i+1];
                a[i] -=a[i+1];
                swapp = true;
            }
        }
    }
}

void printVector(vector<int> a){
    for (size_t i=0;  i <a.size();  i++) {
        cout<<a[i]<<" ";

    }
  cout<<endl;
}
like image 166
Cinch Avatar answered Oct 27 '25 21:10

Cinch


You are passing the vectors as values to your functions, meaning that you are sorting a copy, not the original vector, and then printing a copy of the original vector.

Change the parameter to vector<int> &a in the bubbleSort function, and to vector<int> const &a in the printVector function (as you don't need to change the vector content from here).

By the way, your code may be affected by undefined behavior caused by signer integer overflow. You should use another method to swap your elements, std::swap for example:

std::swap(a[i], a[i + 1]);
like image 44
rems4e Avatar answered Oct 27 '25 21:10

rems4e