# Reliably Broken Sort

Given a list of positive integers that contains at least 3 distinct entries, output a permutation of that list that isn't sorted in ascending or descending order.

### Examples

1,2,3 -> 2,1,3 or 3,1,2 or 1,3,2 or 2,3,1
1,2,3,3 -> 2,1,3,3 or 3,1,2,3 or 1,3,2,3 etc..


Thanks @Arnauld and @NoOneIsHere for the title!

• Will the input always be sorted? – xnor Aug 13 '17 at 20:31
• Must the sort be "reliable" in that given a given set of entries, it always produces the same permutation as output? Or must it only be "reliable" in that the output is not sorted? – Wildcard Aug 15 '17 at 3:16
• It must just satisfy the specs. – flawr Aug 15 '17 at 14:58
• Would a nested array be allowed as output? e.g., [2,[1,3]]. – Shaggy Aug 19 '17 at 9:51
• No, it should be one single array/list. – flawr Aug 19 '17 at 11:47

# Common Lisp, 52 bytes

(let((x(sort(read)'<)))(rotatef(nth 0 x)(nth 1 x))x)


Try it online!

# C# (Mono), 76 67 bytes

using System.Linq;a=>a.OrderBy(n=>n).Skip(1).Concat(new[]{a.Min()})


Saved 9 bytes thanks to @Patrick Stephansen.

Try it online!

• Save a few bytes: a=>a.OrderBy(n=>n).Skip(1).Concat(new[]{a.Min()}) – Patrick Stephansen Aug 15 '17 at 10:42

# Clojure, 36 bytes

#(let[s %](cons(last s)(butlast s)))


Stole the solution to sort it and push the last element to the start of the list.

# TeX - 207 183 bytes

\def\a#1 #2 #3 #4 {\ifnum#2#1#3\def\e{#3 #4 #2 }\else\def\e{#2 #4 #3 }\fi\e}\def\b#1 #2 #3 {\a< #1 #2 \a> #3 {} }\def\c#1 #2 {\ifnum#1=#2\let\b\f\def\d#1 {#1 \c}\else\let\d\b\fi\d#1 #2 }\def\f#1 #2 {#2 #1 }


This is the best I can do with a language that doesn't really have data structures, or many builtins.

The approach is to take the first three numbers we find, and do a broken sort as follows: Sort the first two by greater, and then take the second two by lesser. This should always result in our list being unsorted. It's convenient to ignore anything but the first 3 unique inputs.

We also have a check for two of the same number in a row. In that case, we scan ahead until we find a different number, and then switch the two, ensuring that our list is out of order.

I'm gonna work on golfing this down, but it's the best approach I can think of.

EDIT: Considerable work has been put in to this, to make it more golfy and also actually work. So here it is:

\let~\def~\a#1 #2 {\if^#2~\e{\box9 #1 }\else\ifnum#1\h#2~\e{\hbox{#1 }\a#2 }\else~\e{\hbox{#2 }\a#1 }\ifnum#1=#2~\h{=}\setbox9=\lastbox\fi\fi\fi~\g{>}\e}~\g{<}~\h{\g}\everypar{\a}\x^


I changed the the method from inserting things into the input to one where we recurse, replaced \expandafter with \everypar, and added that when it finds an equal number, it grabs the last number typeset and saves it until the second to last slot, ensuring that 1 2 2 (the case that secretly broke the previous answer) will be incorrectly sorted as 2 1 2.

This is just a function, to properly use it you need to: have input stored on the variable \x (or you could type in a list of numbers, with a ^ as a delimeter).

If you'd really like to try this, then do this

\read1to\x


before the lines of this function.

# Ruby, 20 bytes

->(x){x.sort.rotate}


I can't figure out how to use lambdas on TIO, but I'll add a link when I do

# SmileBASIC, 33 bytes

DEF B A
SORT A
PUSH A,SHIFT(A)END


Modifies the array in place. This uses the simple sort+rotate algorithm, though there may be a shorter solution involving RINGCOPY or something (the problem being that COPY and RINGCOPY don't work well when copying between overlapping parts of the same array)