
Visualizing and exploring sorting algorithms in two dimensions - thesephist
https://dotink.co/posts/visualizing-sorting-algorithms/
======
thethirdone
> Most languages’ baseline sort operations implement quicksort under the hood,
> including the standard C library’s gsort() and JavaScript’s
> Array.prototype.sort()

There isn't any "JavaScript's Array.prototype.sort()". Different
implementations use different algorithms.

I know V8 used to use Quicksort with insertion sort for (sub)arrays smaller
than 10 elements (the implementaion has changed since I last looked, but I
don't know what).

But SpiderMonkey has and still does use a bottom up mergesort with insertion
sort for subarrays of length 3.

~~~
thethirdone
A cursory look at the V8 source seems to show that it delegates sorting to
std::sort which in libstdc++ (gcc) is Introsort (a hybrid Quicksort +
Heapsort).

~~~
Leszek
A more detailed look will show that it uses Timsort:

[https://cs.chromium.org/chromium/src/v8/third_party/v8/built...](https://cs.chromium.org/chromium/src/v8/third_party/v8/builtins/array-
sort.tq)

~~~
thethirdone
Well I don't know how someone not used to the code was suppose to find it at
that odd location.

Thanks for showing me where its actually implemented.

~~~
Leszek
Yeah, for someone not used to the code it's tough. The "insider knowledge" is
that builtins defined in .tq files (files compiled by the Torque compiler[0])
are added to entries in the Builtins enum, which itself is defined as a giant
macro[1] that pulls in a Torque generated macro[2]. In the bootstraper (which
sets up the standard library), the Builtins::kArrayPrototypeSort builtin is
installed as "sort" onto Array.prototype[3], which means you should grep for
"ArrayPrototypeSort", which is defined in the linked .tq file.

[0] [https://v8.dev/docs/torque](https://v8.dev/docs/torque) [1]
[https://cs.chromium.org/chromium/src/v8/src/builtins/builtin...](https://cs.chromium.org/chromium/src/v8/src/builtins/builtins-
definitions.h) [2]
[https://cs.chromium.org/chromium/src/out/Debug/gen/v8/torque...](https://cs.chromium.org/chromium/src/out/Debug/gen/v8/torque-
generated/builtin-definitions-tq.h) [3]
[https://cs.chromium.org/chromium/src/v8/src/init/bootstrappe...](https://cs.chromium.org/chromium/src/v8/src/init/bootstrapper.cc?l=1687&rcl=6d7e1dd21b6495d3b55afae266edb65b41c40b20)

------
sorokod
> _Quicksort is a mouthful to explain in detail_

Don't know about that, this is not efficent but "explains" quicksort in
general:

    
    
        fun <T : Comparable<T>> quickSort(list: List<T>): List<T> =
                with(list) {
                    if (size < 2) this
                    else {
                        val pivot = first()
                        val (smaller, greater) = drop(1).partition { it <= pivot }
                        quickSort(smaller) + pivot + quickSort(greater)
                    }
                }

------
mrami
Selection sort and insertion sort are not O(n) algorithms...

~~~
thethirdone
If swaps are the only thing you are counting, selection sort is O(n).

I can't come up with a reasonable way to consider insertion sort O(n) though.

~~~
mrami
> If swaps are the only thing you are counting, selection sort is O(n).

And if you ignore the bottom half of the centaur, it's just a dude playing a
flute. :)

"In computer science, the time complexity is the computational complexity that
describes the amount of time it takes to run an algorithm." [1] We shouldn't
be confusing people that actually want to learn about this stuff.

[1]
[https://en.wikipedia.org/wiki/Time_complexity](https://en.wikipedia.org/wiki/Time_complexity)

~~~
thethirdone
> And if you ignore the bottom half of the centaur, it's just a dude playing a
> flute. :)

That is an excellent phrase.

> We shouldn't be confusing people that actually want to learn about this
> stuff.

I definitely agree that Selection sort should definitely not be described as
O(n) to beginners. I was just pointing out a subtly. In the strange case where
memory writes are much more expensive than reads or comparisons, it is correct
to say the Selection sort takes O(n) time.

~~~
mrami
I'm not sure what conditions you have in mind, but in the selection sort, the
comparisons are O(n^2), and the assignments are O(n).

If comparisons are a fixed, non-zero cost, and assignments are a fixed, non-
zero cost, then the comparisons in your selection sort will always overwhelm
the assignments, and I can tell you at exactly which input size.

[https://gist.github.com/mrami4/56d6e234f34787b8c5daf543a299a...](https://gist.github.com/mrami4/56d6e234f34787b8c5daf543a299a7d5)

------
rwnspace
Is it just me, shoe-horning this in there, or does this picture [1] of the
Quicksort remind anyone else of a Scutoid? [2]

[1] [https://dotink.co/img/quick-color.png](https://dotink.co/img/quick-
color.png)

[2]
[https://en.wikipedia.org/wiki/Scutoid](https://en.wikipedia.org/wiki/Scutoid)

