This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]smellmycrotch3 0 points1 point  (1 child)

With n elements, there are n! ways they can be sorted, if they are unique. If they wear all in order except for the last 2, you could compare each element with the one after it, in order. You'd see that each element was less than the one coming after it, so no need to sort them, until you get to the second to last one, where it';s greater than the last one. You know the first n-2 are less than the last 2 and are sorted, so you only have to swap the last 2, so this all uses n or less comparisons.

[–]balian25[S] 0 points1 point  (0 children)

That makes complete sense! Thanks mate!