The TeXbook describes (in chapter 14) in detail how TeX calculates the total demerits associated with breaking a paragraph into lines and that TeX chooses the sequence of breakpoints that yield the fewest total demerits (in a process with three passes).
However, there is a tiny detail that is not mentioned in the TeXbook: What happens if two (or more) sequences of breakpoints yield the same total demerits? Can one describe in simple terms which sequence of breakpoints TeX chooses in such a case? Or do you have to read the TeX source code to understand that?
\raggedright. When using\raggedright,\rightskipis infinite, so all lines have zero badness. TeX usually will not consider hyphenation (as that adds penalty) and use as few lines as possible (because of\linepenalty). But there may be quite a lot of different sequences of breakpoints that satisfy these conditions and then have the same total demerits. So\raggedrightprobably quite heavily relies on how TeX handles such a scenario. – user227621 Oct 11 '22 at 14:33texdoc texby the way.) Intuitively I'd guess it tries to fit as much as possible on the first line, then as much as possible on the second line, etc. – user202729 Oct 11 '22 at 16:14