Could anyone please explain how to write mycompare function used in in-built sort function

in string sort in this code below:


i have printed strings which go into the mycompare function in each iteration.
what is the pattern in which the array of strings is passed to the mycompare function and how do we write the my-compare function then?
similarly
the form biggest number question:

Hello @S19APP-PP0097 ,

If we pass the following input to your code:
4
bat
apple
batman
hello

The output is:
apple bat
batman apple
batman bat
batman apple
hello apple
hello bat
hello batman
apple
batman
bat
hello

If you observe carefully, it is comparing each string with every other string in the manner:

suppose str[n] is an array of string:
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
compare(str[i] and str[j])
}
}

Hope, this would help.
Give a like if you are satisfied.

Yes indeed each string is being compared with another
But what is the pattern in which the strings two at a time are getting passed into the function
For example:
Batsman and apple is getting passed on twice
What is the pattern being followed while passing the strings
Coz only if i know
Which is string a and string b
In one particular iteration
I would be able to write the return statements on my own
How will i know that in second return statement
I ought to write a < b
And not the opposite
Coz Otherwise i am just writing the return statements and then changing signs to match my output

For input:
6
bat
apple
dat
hello
batman
hell

Output:
apple bat
dat apple
dat bat
hello apple
hello dat
batman apple
batman hello
batman dat
batman bat
batman apple
hell apple
hell hello
apple
batman
bat
dat
hello
hell

The following picture explains the pattern/ flow of element pass.

  1. I have numbered the comparisons,
  2. The order of number shows the order in which comparison is taking places,
  3. In case of true, swapping occurs and i have shown the sequence after swapping.

52

Observations:

  1. it send the string in the right side in sequence as a and element at left side in sequence as b.

  2. Consider comparison (7) in the figure:
    In case if compare returns true, it performs swap and take the element at left after swap as current element(i.e. a=batman) and compare it the element just before it(b=dat) and continues the swap until compare returns false.

  3. Consider comparison (2),(4),(6),(11) in the figure:
    In case if compare returns false, it make the first element from left which not yet traversed(i.e., a=dat or a=hello or a=batman or a=hell) as the current element(i.e a) and then
    …compares it with the first element of the sequence
    …compares it with the element just before it(i.e a).

Hope, this would help to certain level.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.