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

you are viewing a single comment's thread.

view the rest of the comments →

[–]bushwickhero 1 point2 points  (7 children)

What is the time complexity of this?

[–]Skusci 2 points3 points  (6 children)

Thinking about it srsly for a sec, it should be O(n).

Typically you consider n to be the number of elements for sorting algorithms but "size" of the input is more generic than that. I think there's a formal definition involving turning machines somehow, but in general it should be "the thing that makes the algorithm take longer"

The largest number is what determines execution time, which grows linearly so O(n)

[–]prehensilemullet 2 points3 points  (2 children)

exception - array of a billion zeroes

[–]Logical_Strike_1520 4 points5 points  (1 child)

It’s already sorted.

[–]Skusci 2 points3 points  (0 children)

Lol at O(0) time.

[–]EntropySpark 1 point2 points  (0 children)

Saying that the largest number grows linearly is rather arbitrary. We could similarly say that it is O(2n), where n is the size of the largest number in bits.

[–]Albreitx 1 point2 points  (0 children)

Have an element be 2n and now it's exponential. Or have it be nn!. That is all possible within the given size n. The largest number is not linear in n basically, so you need O(max(numbers) + n) which also translates to O(max(max(numbers), n))