# A “Sorting” algorithm

There is a "sorting algorithm" sometimes called Stalin sort in which in order to sort a list you simply remove elements from the list until it is sorted in increasing order. For example the list

[1, 2, 4, 5, 3, 6, 6]


When "sorted" using Stalin sort becomes

[1, 2, 4, 5, 6, 6]


The three was removed because it was out of order.

Now obviously there are many ways to remove elements to sort a list. For example any list with less than two elements must be sorted so by just removing enough elements blindly we can always sort a list. Since this is the case we only care about the longest possible result from a Stalin sort.

Your task will be to take a list of positive integers and output the length of the longest sorted (increasing) list that can be arrived at by removing elements from the original list. That is find the length of the longest sorted (possibly non-contiguous) sub-list.

Sorted lists can have the same element more than once in a row. You do not need to support the empty list unless your program itself is empty.

# Scoring

Your answer will be scored by the length of its own longest possible Stalin sort. Programs will be interpreted as a sequence of bytes rather than characters, and their order will be the natural one that arises by interpreting the bytes as numbers. Lower scores are better.

# Test cases

[1, 2, 4, 5, 3, 6, 6] -> 6
[19, 2] -> 1
[3, 3, 4, 3] -> 3
[10] -> 1
[1, 2, 4, 9] -> 4
[1, 90, 2, 3, 4, 5] -> 5
[1, 90, 91, 2, 3, 4, 5] -> 5

• In short: output the length of the longest (non-strictly) increasing sequence. – user202729 Oct 30 '18 at 15:16
• I like the rule "You do not need to support the empty list unless your program itself is empty." – Paŭlo Ebermann Nov 1 '18 at 0:24
• This challenge reminds me a lot of the dropsort challenge: codegolf.stackexchange.com/questions/61808/… – Stefnotch Nov 1 '18 at 20:24
• I made a checker at ptpb.pw/SVSt.html . Still not very functional, but it works. (TODO: * bar chart * partition into least decreasing sequences * support for other code pages) – user202729 Nov 2 '18 at 16:17
• @user202729 Cool! I've added it to the post. Feel free to edit newer versions in if necessary. – Sriotchilism O'Zaic Nov 2 '18 at 21:37

# Python 2, length 141210 9

M=max;X=exit;i=input();L=[0]*M(i)
for	a	in	i:L[a-1]=M(L[:a])+1
X(M(L))


Output is via exit code.

Try it online!

### How it works

At all times, the array $$\L\$$ keeps track of the longest sorted subarrays encountered so far; $$\L[a-1]\$$ is the length of longest one that ends with $$\a\$$.

Initially, we haven't processed an array elements, so $$\L\$$ consists entirely of zeroes.

When processing the array element $$\a\$$, we first take the maximum of $$\[L[0], \dots, L[a-1]]\$$, which is length of the longest sorted subarray encountered so far that ends with $$\a\$$ or a smaller integer. Appending $$\a\$$ to such an array will keep it sorted, so the longest sorted subarray ending in $$\a\$$ is one element longer than that maximum. We update $$\L[a-1]\$$ with the computed value.

The final result is the maximum of $$\L\$$.

• Can you please explain, why it works? I'm having hard times to comprehend it :( – Dead Possum Oct 31 '18 at 14:01
• I've added an explanation. – Dennis Oct 31 '18 at 15:54

# Wolfram Language (Mathematica), score 9

Length@*LongestOrderedSequence


Try it online!

# Jelly, length  4  2

ṢƑƇZLƲ}ŒP


Try it online!

### Bytes in Jelly's code page

183 146 144 90 76 169 125 19 80


### How it works

ṢƑƇZLƲ}ŒP  Main link. Argument: A (array)

ŒP  Powerset; yield P, the array of all sub-arrays of A.
}    and apply the chain to the right argument (P).
Ƈ            Comb; only keep arrays for which the link to the left returns 1.
ṢƑ             Sort fixed; yield 1 if sorting doesn't alter the array.
Z           Zip; read the filtered powerset by columns.
L          Take the length.


# Perl 6, score 9

*.combinations.map({@^a*[<=] @a}).max


Try it online!

# Haskell, Score 8 7, 48 bytes

(l:k)%d|d>l=k%d|3>2=max(1+k%l)(k%d)
c%b=0
a=(%0)


Try it online!

The longest sorted sublist is

%%%%%%)


# Pyth, score 3 2 (7 bytes)

leSI#y


Saved a point thanks to Anders Kaseorg.
Try it here

### Explanation

leSI#y
yQ    Take the power set of the (implicit) input (preserving order).
SI#      Get the ones that are sorted.
e         Take the last (longest).
l          Get the length.

• leSI#y scores 2. – Anders Kaseorg Oct 30 '18 at 21:53

# Stax, 4 maximal length stalin sort

S{:^fF%|M


Run and debug it

It works like this.

S       powerset of input
{:^f    filter by non-descending sequences
F%|M    take the maximum length remaining


# R, Score 15 11, 72 62 bytes

function(L,M=max,A=1:M(L)*0){for(Y in L)A[Y]=M(A[1:Y])+1;M(A)}


Try it online!

Ports Dennis' Python answer to R.

• Just changing variable names won't help, because as your last link shows, none of them are used in the (found) substring that gives score 15. – Ørjan Johansen Oct 31 '18 at 1:10
• @ØrjanJohansen ah, of course, I'm rather dumb. I suppose another approach is necessary. – Giuseppe Oct 31 '18 at 18:48