#Big O Notation .sort (ArrayList)
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
The Big O notation for the .sort() method used to sort an ArrayList in Java is O(n log n). This means that the time complexity of the sorting algorithm used by .sort() is proportional to n multiplied by the logarithm of n, where n represents the number of elements in the ArrayList. Overall, this indicates that as the size of the ArrayList increases, the time taken to sort it will increase at a slightly slower rate than a linear relationship.
It's important to note that different sorting algorithms may have different time complexities. The default sorting algorithm used by .sort() in Java is a variant of TimSort, which has an average time complexity of O(n log n) and a worst-case time complexity of O(n log^2 n).
Therefore, when using the .sort() method on an ArrayList in Java, you can generally expect it to have a time complexity of O(n log n), making it an efficient choice for sorting large collections of elements.
Check the doc of the method
AI response was pretty on point too
the time taken to sort it will increase at a slightly slower rate than a linear relationship
it should be "faster rate"
otherwise it implies that the complexity is better than linear, which it's not
arraylists use TimSort so they're a standard O(n log n) sort
some array sorts are faster. byte[]'s sort uses counting sort if it's big enough, which is O(n)
also comparison based sorting algorithms can never be faster than O(n log n)
*in the worst case
They can be linear if everything is sorted already (or reversed, both handled in linear time by timsort)
Big O implies worst case
its called Omega(n log n) btw, i.e. a lower bound. not an upper bound
This is a video compares Mergesort to Timsort on data which is already mostly linear. It was inspired by this video ( https://www.youtube.com/watch?v=NVIjHj-lrT4 ) and a comment on that video by Raymond Hettinger asking for data which better shows off the advantages of Timsort. My example of real world data is data in which the first 90% is alre...
I've had discussions online about this, so I'd like to specify
Only when talking about Big Omega Notation. Those are two different things
stating "never be faster than O(n log n)" is a weird thing to say
since f in O(n log n) means "f is faster/equals than n log n"
(in terms of asymptotic growth ofc)
should have said better rather than faster
i want to avoid going into too much detail as this is alex thread and they are probably confused by all our "expert talk" anyways - after all they havent said a single thing since this thread started
Sort algo can never be faster than O(n), many are O(nlogn) or similar
for e.g. quicksort you can calculate the expected runtime which is asymptotic O(n log n) but the worst case is O(n^2) so just because you use the O notation that does not mean that is the worst case runtime
also does anyone know why java uses timsort? Quicksort should be faster except the data is nearly sorted or am i wrong?
people always get this stuff wrong
big-O implies worst case. it has a "for all n" in it
when u read online about "but its worst case O(...)" then what these people actually do is not a general big-o analysis anymore but they restrict the input domain from "all inputs" to only the inputs that are bad for the algo. for example "all already sorted inputs are allowed now only" (what kind if input domain is the worst depends on the algo in question ofc). and then, after restricting the input domain they perform a big-o analysis
and that's what yields those statements
dont focus on big-o for this stuff.
timsort (and javas is modified further) is currently the sorting algo known to perform by far best on inputs met in real applications
what they were referring to earlier is the proven (easy proof) lower bound for so called comparison based sorting, which is Omega(n log n).
that means its impossible to find a comparison based sorting technique that performs any better than n log n on arbitrary input, wrt to asymptotic growth
@plush relic Fun fact: Nem specifically said "comparison based" because there are non-comparison based sorting algorithms that can run faster, e.g. Radixsort which runs in O(n * k) where n is the number of keys and k is the length of the longest key.
*Radixsort has a space complexity of O(n + k).
sleep sort
timsort does less comparisons, which, in object oriented languages like java, take more time
they do use quicksort (as a fallback) for primitive arrays
well, quicksort with insertion sort
There's been a lot of performance tuning in those sorting algorithms, so depending on the size of the input, the type of array etc, they will use different strategies
no but this is not the case for quicksort. e.g. randomised quicksort. you can say the runtime is a Random variable and then calculate the expected vaule of that depending on the input length. you could calulate that exact but since asymtotic is the standard you can estimate it upwards. and then you have E[runtime] <= c * n log n = O(n log n), because the probability for choosing always the largest or smallest element is so incredibly low, so the worst case is these one 0.000000000000000000...0001% probablity that for every partition the largest or smalles element was choosen as pivot. big O is a asymtotic notation you can e.g. say the expected (average) runtime of quicksort is in O(n log n) which makes sense for a las vegas algorithm like randomized quicksort
we really have to move this out of this thread.
so all i want to say for now is that what ur saying isnt entirely correct, mathematically speaking.
ive been doing big-o and developing and analyzing algorithms academically for ~5 years.
people without the full mathematical background always get the terminology wrong, its a meme in the "expert community"
we can continue this in #geek-speak in more detail if u want
lets wrap this thread up for now and leave it to alex to ask if there are any more questions
