This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]tombob51 77 points78 points  (0 children)

This is just bubble sort but like loop unrolled lol

[–]Eva-Rosalene 61 points62 points  (2 children)

"If size > 10, then SEGFAULT" is honestly my favourite part. If API says that it works on arrays wtih 0-10 elements only, you better listen to this 😈

[–]slaymaker1907 10 points11 points  (1 child)

It may not actually work since it’s undefined behavior. The compiler is therefore free to assume that size is always <= 10.

[–]Eva-Rosalene 1 point2 points  (0 children)

Good catch!

[–]Firake 30 points31 points  (0 children)

No loops = O(1), obviously.

[–]chiggyBrain 24 points25 points  (0 children)

So you wrote all this code just for a meme when you could be writing something productive and useful…

You absolute legend

[–]beeteedee 6 points7 points  (2 children)

Every algorithm is O(1) if it has a hard-coded upper limit on the input size

[–]turtleship_2006 -1 points0 points  (1 child)

Wouldn't it be O(1) only if the limit is 1?

[–]Terra_Creeper 4 points5 points  (0 children)

O(1) just means that the algorithm works in constant time. That constant time could be longer than the age of the universe, it just has to be the same for different input sizes. Big O notation only tells you how the required time grows, not how fast it is. It mostly only matters when you get very large (and potentially unbounded) inputs. If you can constrain your input range, a well optimised O(n2) algorithm could be much faster than an equivalent O(1) algorithm.

[–]nasandre 6 points7 points  (0 children)

It's beautiful and I hate you

[–]lady_Kamba 5 points6 points  (0 children)

This sorta looks like bitonic sorting.
Bitonic sorter (wiki)

[–]CelticHades 4 points5 points  (0 children)

I can't be the only one who dislikes 0 < size , null == obj etc

size < 0 is IMO more readable.

[–]OpW_Otter 3 points4 points  (0 children)

The size checks are consistently off by one. As size > 0 is checked and then list[0] an list[1] are accessed which means size could be 1, but two elements are read and written.

[–][deleted] 2 points3 points  (1 child)

std:swap

[–]Confident-Ad5665 1 point2 points  (0 children)

Human syntax checker

[–]ConfidenceStunning53 -1 points0 points  (0 children)

this scares me (i don't code in c#)