44
2
Challenge
Given a non-empty array of integers, e.g.:
[5, 2, 7, 6, 4, 1, 3]
First sever it into arrays where no item is larger than the previous (i.e. non-ascending arrays):
[5, 2] [7, 6, 4, 1] [3]
Next, reverse each array:
[2, 5] [1, 4, 6, 7] [3]
Finally, concatenate them all together:
[2, 5, 1, 4, 6, 7, 3]
This should be what your program outputs/function returns. Repeat this procedure enough times and the array will be fully sorted.
Rules
- Input and output may be given through any standard methods, and may be in any reasonable array format.
- The input array will never be empty, but may contain negatives and/or duplicates.
- The absolute value of each integer will always be less than 231.
Test cases
Hopefully these cover all edge cases:
[1] -> [1]
[1, 1] -> [1, 1]
[1, 2] -> [1, 2]
[2, 1] -> [1, 2]
[2, 3, 1] -> [2, 1, 3]
[2, 1, 3] -> [1, 2, 3]
[2, 1, 2] -> [1, 2, 2]
[2, 1, 1] -> [1, 1, 2]
[3, 1, 1, 2] -> [1, 1, 3, 2]
[3, 2, 1, 2] -> [1, 2, 3, 2]
[3, 1, 2, 2] -> [1, 3, 2, 2]
[1, 3, 2, 2] -> [1, 2, 2, 3]
[1, 0, 5, -234] -> [0, 1, -234, 5]
[1, 0, 1, 0, 1] -> [0, 1, 0, 1, 1]
[1, 2, 3, 4, 5] -> [1, 2, 3, 4, 5]
[5, 4, 3, 2, 1] -> [1, 2, 3, 4, 5]
[2, 1, 5, 4, 3] -> [1, 2, 3, 4, 5]
[2, 3, 1, 5, 4] -> [2, 1, 3, 4, 5]
[5, 1, 4, 2, 3] -> [1, 5, 2, 4, 3]
[5, 2, 7, 6, 4, 1, 3] -> [2, 5, 1, 4, 6, 7, 3]
[-5, -2, -7, -6, -4, -1, -3] -> [-5, -7, -2, -6, -4, -3, -1]
[14, 5, 3, 8, 15, 7, 4, 19, 12, 0, 2, 18, 6, 11, 13, 1, 17, 16, 10, 9] -> [3, 5, 14, 8, 4, 7, 15, 0, 12, 19, 2, 6, 18, 11, 1, 13, 9, 10, 16, 17]
Scoring
This is code-golf, so the shortest code in bytes wins.
4What's the big-o of this sorting method? – mbomb007 – 2016-12-21T22:08:58.790
1@mbomb007 I don't understand big-o notation very well, but I think a single iteration is O(n). Multiply that by worst-case n iterations and you get O(n^2) (worst-case; best-case would be O(n), I think, for a single iteration). – ETHproductions – 2016-12-21T22:23:25.517
1That sounds right to me, however it's worth pointing out that reversing an array isn't a very efficient operation, so it's a slow
O(n^2)
– James – 2016-12-21T22:45:21.8032@WheatWizard reversing an array doesn't require room for a copy of the array, only room for a single element. and is
O(n)
. swap first and last elements then swap second and second last elements etc, when you get to the middle stop. – Jasen – 2016-12-22T11:27:02.980Reversing is
O(n)
, but reversing can be built right into the algorithm (that's what my JS answer does); since each iteration loops over each item in the array once, a single iteration isO(n)
. (I think...) – ETHproductions – 2016-12-22T14:27:26.237It's definitely
O(n^2)
. Rotating a sorted array by 1 causes causes theO(n^2)
lower bound ([2,3,4,5,6,1]
). However the upper bound is alsoO(n^2)
(assuming a good implementation), as it is strictly better than bubble sort (as it swaps two numbers + possibly more) – Nathan Merrill – 2016-12-22T16:35:07.353Here are the average number of severs for random list sizes from 1-20. Each one was run 10000 times, with the total severs counted, and then an average calculated. Note that the "random lists" were filled with uniform random elements 1-100.
– FlipTack – 2016-12-22T17:54:50.033