Search code examples
c++vectorbubble-sort

C++ bubble sort on vector using callback switch


I have been trying to bubble sort a vector of int data which has been taken from a multi line text file. I have managed to seperate the data from the vector so that I have only the elements I need to sort, however I am having an issue getting the code to run using a vector. I am reusing code I have made earlier when sorting an array but can't seem to get it to play nicely with vectors.

I have a seperate class for the sorting:

#pragma once
#include <iostream>
#include <vector>

using namespace std;

class Sorter {
public:
    int checker;

    //Callback function for bubble sort
    int compare(vector<int> a, vector<int> b) {
        //Do decending sort
        if (a > b) {
                return -1;
        }
        //Do ascending sort
        else {
            return 1;
        }
    }

    //Bubble sort - best case complexity omega(n)
    void bubbleSort(vector<int> &A, int n, int(*compare)(int, int)) {
        int i, j, temp;
        for (i = 0; i < n; i++){
            for (j = 0; j < n - 1; j++) {
                if (compare(A[j], A[j + 1]) > 0) {
                    temp = A[j];
                    A[j + 1] = temp;
                }
            }
        }
    }
};

And the part of my main source file that calls it:

Sorter sorter;
sorter.checker = inputVector[0];
vector<int> sortingVector;
cout << inputVector.size() << endl;
for (int i = 3; i <= inputVector[2]; i++) {
    sortingVector.push_back(inputVector[i]);

}

sorter.bubbleSort(sortingVector, inputVector[2], sorter.compare());

I'm getting the error: no suitable conversion function from "std::vector<int, std::allocator>" to "int" exists. This makes me think I either:

  1. Can't use vectors or,
  2. I have converted it to the wrong data type.

If anyone could help me work this out that would be amazing. Thanks!


Solution

  • Your compilation error is because you have called the comparison function with no arguments, and not passed it as a parameter.

    You also have the issue that a non-static member function requires a Sorter (which this will point to).

    The parameters of the comparison function need to be elements of the vector.

    I would advise not using a class to hold free functions

    int compare(int a, int b) {
        if (a > b) {
                return -1;
        }
        else {
            return 1;
        }
    }
    
    void bubbleSort(vector<int> &A, int(*compare)(int, int)) {
        for (int i = 0; i < A.size(); i++){
            for (int j = 0; j < A.size() - 1; j++) {
                if (compare(A[j], A[j + 1]) > 0) {
                    std::swap(A[j], A[j + 1]);
                }
            }
        }
    }
    

    Which you call as:

    bubbleSort(sortingVector, compare);
    

    Aside: if you were to use std::sort, you wouldn't need to copy from inputVector to sortingVector`

    if (ascending) {
        std::sort(inputVector.begin() + 3, inputVector.end(), std::less<int>{});
    } else {
        std::sort(inputVector.begin() + 3, inputVector.end(), std::greater<int>{});
    }