# Implement Lazy Drop Sort

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.

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 , fewest bytes wins!

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 Mar 23 '18 at 0:40
• @rafa11111 Either is fine – Pavel Mar 23 '18 at 0:44
• 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 Mar 23 '18 at 1:56
• @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 Mar 23 '18 at 2:08

# Retina 0.8.2, 32 31 bytes

\d
$* r(?<=1\1) (1+)\b 1+$.&


Try it online! Link includes test cases. Explanation: The first and last stages are simply unary conversion. The middle stage deletes the numbers that are preceded by a larger number. A right-to-left match is used so that the lookbehind can be placed at the start of the regex, saving 1 byte.

# Python 2, 50 49 bytes

f=lambda s:s and f(s[:-1])+s[-1]*(s[-2:]<s[-1]*3)


I/O is on strings.

Try it online!

# C, 82 bytes

f(a,c,i,j)int*a;{for(i=-1;++i<c;j=a[i])printf("%d "+3*(i&&j>a[i]),a[i]);puts("");}


Try it online!

I used the usual tricks (using K&R-style functions, indexing into a constant string, etc.) and because I don't like ending output on the same line as the next program added a newline. To make sure that the 1-element arrays were handled correctly, I special-cased it.

# Fortran (GFortran), 74 bytes

SUBROUTINE D(I,J)
INTEGER I(J)
DO1 K=1,J
1 IF(I(K)>=I(K-1))PRINT*,I(K)
END


This is my entry in this lovely language. It simply prints the "sorted" array, doesn't return it. I is the array to be "sorted" and J is its length. The TIO link below shows a complete program (with 160 bytes) using the subroutine. The input array is hardcoded in the main program.

Actually, I think that there might be some way to get an unknown length array as input, even from STDIN, using allocatable arrays, but it would be a really ugly program. Anyway, it seems fair to be lazy while implementing a lazy algorithm in a aged (and, therefore, tired) programming language :).

Conclusion: Fortran is an awesome language for arrays, as far as you know their lengths.

Try it online!

SHELL ( 45 Bytes)

Solution 1 : output on 1 line ( 57 Bytes)

L(){ for i in $*;do(($i>=$o))&&echo$i;o=$i;done|xargs;}  Test : >L 8 6 9 9 7 2 3 8 1 3 8 9 9 3 8 3  Solution 2 : output 1 element / line ( 51 Bytes) L(){ for i in$*;do(($i>=$o))&&echo $i;o=$i;done;}


Test :

>L 8 6 9 9 7 2 3 8 1 3
8
9
9
3
8


Solution 3 : with bc ( 51 Bytes)

 L(){ for i in $*;do bc<<<"if($i>$o)$i";o=$i;done;}  Solution 4 : optimisation ( suppression in$*, thanks user17752 ) ( 45 Bytes)

 L(){ for i;do bc<<<"if($i>$o)$i";o=$i;done;}

• I don't think you need the space after L, or xargs. – Pavel Mar 22 '18 at 23:22
• Also, which shell is this? I get a syntax error in Bash: – Pavel Mar 22 '18 at 23:31
• for i in \$* can be shortened to for i – DarkHeart Mar 23 '18 at 6:42
• Tested on Cygwin ( CYGWIN_NT-10.0 2.3.1(0.291/5/3) ) – Ali ISSA Mar 23 '18 at 22:02

# C (clang), 53 bytes

x,c;f(char *s){for(x=0;c=*s++;x=c)c>=x?putchar(c):0;}


Try it online!

Takes input as string of digits, For all digits, prints it if greater than previous.

• Suggest c<x||putchar(c); instead of c>=x?putchar(c):0;` – ceilingcat Oct 4 '18 at 17:22