How to Loop by cutting list then reversing?

Hello,

I am stuck with one question, which is a looping question. I need to write an algorithm. I have the initial list as being {3, 2, 5, 1, 4}. Then, we can “cut” the list at 5 (consider the sublist {3,2,5}) and reverse it. Then, the list became {5, 2, 3, 1, 4}. Now we could decide to flip the whole list and obtain {4,1,3,2,5}.and so on. I need to loop the code until the code is in order which is {1,2,3,4,5}
This is what I have now

tl = {3, 2, 5, 1, 4};

For[
 i = 4, i < 5, i++;
  stry = tl;
  
  newlist = Reverse[stry[[1 ;; 3]]];
  
  sstry = 
   DeleteDuplicates[Flatten[AppendTo[newlist, stry[[1 ;; i]]]]]; |
  Print[sstry]
   Print[Reverse[sstry]]
 ]

It is not clear what you mean by

and so on

In the next iteration is the list again cut at the position where 5 is, or cut at index 3? Either way, why do you expect this sequence of operations to sort the list?

Sadly, that is the task I received. I think the task is to try and ask if the loop can be in order of {1,2,3,4,5} in the end.

In that case, you need to clarify the problem with whoever gave it to you. Using a fixed index on each iteration results in a cycle.

ClearAll@flipIt
flipIt[l_List, pos_Integer] := Reverse@l[[1 ;; pos]]~Join~l[[pos + 1 ;;]] // Reverse

Test that the first iteration reproduces the list in the question

flipIt[tl, 3]
(* {4, 1, 3, 2, 5} *)

Repeat until a cycle is detected

f = NestWhileList[flipIt[#, 3] &, #, Unequal, All] &
f@tl
(* {{3, 2, 5, 1, 4}, {4, 1, 3, 2, 5}, {5, 2, 4, 1, 3}, {3, 1, 5, 2, 4}, {4, 2, 3, 1, 5}, {5, 1, 4, 2, 3}, {3, 2, 5, 1, 4}} *)

The initial list has been repeated and will continue to repeat in the same cycle.

You can modify the code to see what happens if the “cut” position is based on value rather than index.

Thank you!

Hi, thank you for the help. Do you mind explaining a little about how you created the first function? Thank you in advance.

Similar question posted here.

What do line separators and files have to do with the original question?