Recursion Dictionary Order(Larger)

the last test case is passing only if i print the answer after the recursive i.e. in decreasing order unlike other test cases…i think order of the answer of the last test case is wrong…please see it…
and similarily for dictionary order (smaller) the output of sample test case is not in dictionary order…

here is my code for dictionary order (larger)—

#include<bits/stdc++.h>

using namespace std;

int search(string str,int s,int key){
int e=str.length()-1;
int mid,ans;
while(s<=e){
mid=(s+e)/2;
if(str[mid]<=key)
e=mid-1;
else{
ans=mid;
s=mid+1;
}
}
return ans;
}

void next(string s){
int i=s.length()-2;

while(i>=0&&s[i]>=s[i+1])
i--;
if(i<0)
return ;
else{
	int j=search(s,i+1,s[i]);
	swap(s[i],s[j]);
	reverse(s.begin()+(i+1),s.end());
	cout<<s<<endl;
	next(s);
	
}

}

int main(){

string s;
cin>>s;

next(s);

return 0;

}

prajjawal can u send the steps of your Algorithm The Process
As the part of your backtracking process is not clear

i’m not actually backtracking here coz i’m calling functions just by value…

these are the steps though–

1-Find largest index i such that array[i − 1] < array[i].
(If no such i exists, then this is already the last permutation.)

2-Find largest index j such that j ≥ i and array[j] > array[i − 1].

3-Swap array[j] and array[i − 1].

4-Reverse the suffix starting at array[i].

my doubt is still not cleared…
why is the question not accepting answer in the dictionary order

prajawal try for the test case 231
output should be 321
312
ur code
312
321

how could answer be
321
312…???

thats what im saying…does the dictionary order means decreasing???no…its increasing…and this is not increasing…

see output statement it didnt say any order
it say only print the string which are larger in dictonary order than the given string

all other cases are passing as the input is already in sorted order