Bounty Ended with 100 reputation awarded by Adám
7 deleted 87 characters in body

# APL (Dyalog Unicode), 352623 22 bytesSBCS

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}


Try it online!

Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.

The TIO link contains two test cases.

Explanation:

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵                 ⍝ if the array is sorted:
⍵≡⍳≢⍵                 ⍝ array = 1..length(array)
:0               ⍝ then return 0
⋄              ⍝ otherwise
1+            ⍝ increment
∇           ⍝ the value of the recursive call with this argument:
⍵[      ]  ⍝ index into the argument with these indexes:
⍳⍴⍵   ⍝ - generate a range from 1 up to the size of ⍵
2|      ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒        ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.


Old solution: {i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}

¹

# APL (Dyalog Unicode), 352623 22 bytesSBCS

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}


Try it online!

Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.

The TIO link contains two test cases.

Explanation:

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵                 ⍝ if the array is sorted:
⍵≡⍳≢⍵                 ⍝ array = 1..length(array)
:0               ⍝ then return 0
⋄              ⍝ otherwise
1+            ⍝ increment
∇           ⍝ the value of the recursive call with this argument:
⍵[      ]  ⍝ index into the argument with these indexes:
⍳⍴⍵   ⍝ - generate a range from 1 up to the size of ⍵
2|      ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒        ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.


Old solution: {i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}

¹

# APL (Dyalog Unicode), 352623 22 bytesSBCS

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}


Try it online!

Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.

The TIO link contains two test cases.

Explanation:

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵                 ⍝ if the array is sorted:
⍵≡⍳≢⍵                 ⍝ array = 1..length(array)
:0               ⍝ then return 0
⋄              ⍝ otherwise
1+            ⍝ increment
∇           ⍝ the value of the recursive call with this argument:
⍵[      ]  ⍝ index into the argument with these indexes:
⍳⍴⍵   ⍝ - generate a range from 1 up to the size of ⍵
2|      ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒        ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.


¹

6 deleted 7 characters in body

# APL (Dyalog Unicode), 352623 22 bytesSBCS

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}


Try it online!

Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.

The TIO link contains two test cases.

Explanation:

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵                 ⍝ if the array is sorted:
⍵≡⍳≢⍵                 ⍝ array = range(0, 1..length(array))
:0               ⍝ then return 0
⋄              ⍝ otherwise
1+            ⍝ increment
∇           ⍝ the value of the recursive call with this argument:
⍵[      ]  ⍝ index into the argument with these indexes:
⍳⍴⍵   ⍝ - generate a range from 1 up to the size of ⍵
2|      ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒        ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.


Old solution: {i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}

¹

# APL (Dyalog Unicode), 352623 22 bytesSBCS

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}


Try it online!

Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.

The TIO link contains two test cases.

Explanation:

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵                 ⍝ if the array is sorted:
⍵≡⍳≢⍵                 ⍝ array = range(0, length(array))
:0               ⍝ then return 0
⋄              ⍝ otherwise
1+            ⍝ increment
∇           ⍝ the value of the recursive call with this argument:
⍵[      ]  ⍝ index into the argument with these indexes:
⍳⍴⍵   ⍝ - generate a range from 1 up to the size of ⍵
2|      ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒        ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.


Old solution: {i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}

¹

# APL (Dyalog Unicode), 352623 22 bytesSBCS

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}


Try it online!

Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.

The TIO link contains two test cases.

Explanation:

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵                 ⍝ if the array is sorted:
⍵≡⍳≢⍵                 ⍝ array = 1..length(array)
:0               ⍝ then return 0
⋄              ⍝ otherwise
1+            ⍝ increment
∇           ⍝ the value of the recursive call with this argument:
⍵[      ]  ⍝ index into the argument with these indexes:
⍳⍴⍵   ⍝ - generate a range from 1 up to the size of ⍵
2|      ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒        ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.


Old solution: {i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}

¹

5 deleted 137 characters in body

# APL (Dyalog Unicode), 35262323 22bytesSBCS

{∧/2≤/⍵⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}


Thanks to Adám for the help and, Erik the Outgolfer for -3 and ngn for -1.

The TIO link contains two test cases.

Explanation:

{∧/2≤/⍵⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{∧/2≤/⍵⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
∧/2≤/⍵⍵≡⍳≢⍵                 ⍝ if the array is sorted:
2≤/⍵⍵≡⍳≢⍵                 ⍝ - reduce (/) the array in 2-elements= pairrange(0, and check that the 2nd is ≤ than the 1st
∧/                     ⍝ - the condition applies for the whole array length(reduce with ANDarray))
:0               ⍝ then return 0
⋄              ⍝ otherwise
1+            ⍝ increment
∇           ⍝ the value of the recursive call with this argument:
⍵[      ]  ⍝ index into the argument with these indexes:
⍳⍴⍵   ⍝ - generate a range from 1 up to the size of ⍵
2|      ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒        ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.


Old solution: {i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}

¹

# APL (Dyalog Unicode), 352623bytesSBCS

{∧/2≤/⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}


Try it online!

Thanks to Adám for the help and Erik the Outgolfer for -3.

The TIO link contains two test cases.

Explanation:

{∧/2≤/⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{∧/2≤/⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
∧/2≤/⍵                 ⍝ if the array is sorted:
2≤/⍵                 ⍝ - reduce (/) the array in 2-elements pair, and check that the 2nd is ≤ than the 1st
∧/                     ⍝ - the condition applies for the whole array (reduce with AND)
:0               ⍝ then return 0
⋄              ⍝ otherwise
1+            ⍝ increment
∇           ⍝ the value of the recursive call with this argument:
⍵[      ]  ⍝ index into the argument with these indexes:
⍳⍴⍵   ⍝ - generate a range from 1 up to the size of ⍵
2|      ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒        ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.


Old solution: {i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}

¹

# APL (Dyalog Unicode), 352623 22bytesSBCS

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}


Try it online!

Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.

The TIO link contains two test cases.

Explanation:

{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵                 ⍝ if the array is sorted:
⍵≡⍳≢⍵                 ⍝ array = range(0, length(array))
:0               ⍝ then return 0
⋄              ⍝ otherwise
1+            ⍝ increment
∇           ⍝ the value of the recursive call with this argument:
⍵[      ]  ⍝ index into the argument with these indexes:
⍳⍴⍵   ⍝ - generate a range from 1 up to the size of ⍵
2|      ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒        ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.


Old solution: {i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}

¹

4 added 1053 characters in body
3 added 56 characters in body
2 added 76 characters in body
1