Problem with select sort in C++ using swap

I'm trying to finish this code, and right know i'm facing a error with the Selection Sort Algorithm:

error: no matching function for call to ‘swap(const std::vector, const std::vector)’ swap(&adj[min_idx], &adj[j]);

and

invalid conversion from ‘int’ to ‘int’ [-fpermissive] b = temp;*

I have to sort the graph by the number of its vertices's degree. Someone can help me? Thank you!

void swap(int *a, int *b) {    
   int temp;
   temp = a;
   a = b;
   b = temp;
}


void  sortGraph(vector<int> const* adj, size_t count){
  int size = count;
    
    //sort by degree, number of children
         for (int j = 0; j < size - 1; j++) {
            int min_idx = j;
            for (int i = j + 1; i < size; i++) {
              if (adj[i].size() < adj[min_idx].size())
                 min_idx = i;
            }
            swap(&adj[min_idx], &adj[j]);
          }
}


void printGraph(std::vector<int> const* adj, size_t count)
{
    std::vector<size_t> indices;
    for (size_t i = 0; i != count; ++i)
    {
        indices.push_back(i);
    }

    for (auto index : indices)
    {
        std::cout << "Vertex " << index << ", degree " << adj[index].size() << '\n';
    }
}

void addEdge(vector<int> adj[], int u, int v)
{
    adj[u].push_back(v);
    adj[v].push_back(u);
}


int main()
{
int V = 5;
vector<int> adj[V];
addEdge(adj, 0, 2);
addEdge(adj, 0, 1);
addEdge(adj, 0, 3);
addEdge(adj, 0, 4);
addEdge(adj, 2, 1);
addEdge(adj, 4, 1);
//printGraph(adj, V);
sortGraph(adj, V);
return 0;
}

https://ideone.com/mgffaj



Read more here: https://stackoverflow.com/questions/67480441/problem-with-select-sort-in-c-using-swap

Content Attribution

This content was originally published by Thiago Almeida Marques at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: