Why bool (&cmp)(int a,int b) ???
shouldn’t it be bool (*cmp) (int a ,int b)…
Why bool (&cmp)(int a,int b) ???
shouldn’t it be bool (*cmp) (int a ,int b)…
??? when we pass a function…we are passing the address…then have to store it in a pointer variable only …Mine is working fine with bool (*cmp) (int a, int b)…
#include
using namespace std;
bool compare(int a,int b){
return a < b;
}
void bubbleSort(int arr[],int n, bool (*cmp) (int a, int b)){
for(int itr=1;itr<=n-1;itr++){
for(int j=0;j<n-itr;j++){
if(cmp(arr[j],arr[j+1])){
swap(arr[j],arr[j+1]);
}
}
}
return;
}
int main(){
int arr[1000];
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
}
bubbleSort(arr,n,compare);
for(int i=0;i<n;i++){
cout<<arr[i];
}
return 0;
}