26
2
This challenge already describes dropsort. However, I'm kinda lazy and I really only need my array to be a bit more sorted than before, it doesn't need to be sorted all the way.
In Drop Sort, we drop every element less than any element before it. In Lazy Drop Sort, we drop every element less than the one strictly preceding it.
Here's an example. Consider the following array:
8 6 9 9 7 2 3 8 1 3
Let's mark every element less than the one before it.
8 6 9 9 7 2 3 8 1 3
^ ^ ^ ^
Notice how neither 3
was marked, nor the last 8
. They are all larger than the single element to the left of them.
Completing the algorithm, removing the marked elements, we get:
8 9 9 3 8 3
That basically looks more sorted. Kinda. I'm lazy.
Your task, as you may have already deduced, is to implement this algorithm.
Input is an array of at least 1 positive integer between 1 and 9, so you can take a string of digits as well.
This is code-golf, fewest bytes wins!
Additional test cases:
1
1
1 2 3
1 2 3
5 3 1
5
1 2 3 2 1
1 2 3
1 1 1 9 9 9 1 1 1 9 9 9 1 1 1
1 1 1 9 9 9 1 1 9 9 9 1 1
9 9
9 9
5 2 4 2 3
5 4 3
Can it be a function or it must be a complete program? – rafa11111 – 2018-03-23T00:40:11.253
@rafa11111 Either is fine – Pavel – 2018-03-23T00:44:27.107
In the case it is a function, can the input array be hardcoded in the main program? And can the length of the array be passed as input to the function? – rafa11111 – 2018-03-23T01:56:26.380
@rafa11111 The input can't be hardcoded in the function itself. It doens't matter how the function gets this input in your test program. You can take an array length only if you're using C/C++ or another language where that's the only way to determine an array's length. – Pavel – 2018-03-23T02:08:31.053