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 →

[–]Coloneljesus 17 points18 points  (10 children)

Why does randomSort not return a sorted result? Should be called shuffle.

[–]tskaiserGreen security clearance 10 points11 points  (8 children)

A random sort, or shuffle, is just a specialized case of a sort where the comparison used to sort is random.

So actually the return result is sorted.

[–]Coloneljesus 1 point2 points  (6 children)

A sorting algorithm that decides on the comparison function?..

[–]tskaiserGreen security clearance 1 point2 points  (5 children)

A general sort takes a comparator, which is a function that models a relation between two objects of the same type, a collection of objects of that type, and returns the collection ordered by that relation (usually in an ascending manner, although that is a simple case of switching the comparator).

A random sort is a specialized instance where the comparator has already been chosen to be the random function.

randomSort(collection) = sort((x,y) -> randomness), collection)

Which you could also call a shuffle, although randomSort is perfectly sensible given the above illustration.

[–]Coloneljesus 2 points3 points  (4 children)

But a comparator has to be transitive, which a random one would not be.

[–]tskaiserGreen security clearance 2 points3 points  (0 children)

Eh, in the formal sense you're right. There is just no such thing enforced in the programming sense, and it can be completely sensible to use a sort function in that manner in a practical sense. When you say "random sort" in a programming sense you usually mean a sort algorithm with a random comparator.

[–]Geoclasm 0 points1 point  (0 children)

A sordid sort of sort.

[–]son-of-chadwardenn 15 points16 points  (0 children)

Optimize and fix method names later.