Get segmentation fault but when i print the vector it is printing the expected output

vector Solution::nextPermutation(vector &v) {
int i=v.size()-1;
while(i>0){
if(v[i]>v[i-1]){
break;
}
i–;
}
if(i-1<0){
sort(v.begin(),v.end());
}else{
// cout<<"\nbefore sort \n";
for(int j=0;j<v.size();j++){
cout<<v[j]<<" “;
}
sort(v.begin()+i,v.end());
// cout<<”\nafter sort\n";
for(int j=0;j<v.size();j++){
cout<<v[j]<<" “;
}
int temp=v[i-1];
v[i-1]=v[i];
v[i]=temp;
// cout<<”\nafter swap\n";
// for(int j=0;j<v.size();j++){
// cout<<v[j]<<" ";
// }
}
}

Click here to start solving coding interview questions