Learning to implement a C++ BubbleSort using function pointers -
my compare function simple return true or false. bubblesort works going through vector once, sorting once, not going continue sort. if numbers 1, 5, 2, 4, 3, 6, 5, 1, 4, 2, 6, 3. instead of 1, 2, 3, 4, 5, 6. if use bool swapped, there error. here code:
void bsort(vector<int> &vector1, bool (*compare) (int a, int b)) { bool swapped = true; //using swapped in following code causes crash while(swapped) { swapped = false; (int = 0; < vector1.size(); ++i) { (int j = 1; j < vector1.size(); ++j) { if ((*compare)(vector1[i], vector1[j]) == true) { swap(vector1[i], vector1[j]); swapped = true; } } } } }
i have seen function before, , trying implement because want learn. please help.
edit: here fixed , working function! :)
void bsort(vector<int> &vector1, bool (*compare) (int a, int b)) { bool swapped = true; while(swapped) { swapped = false; (int = 0; < vector1.size(); ++i) { if ((*compare)(vector1[i], vector1[i+1]) == true) { swap(vector1[i], vector1[i+1]); swapped = true; } } } }