| ▲ | vprcic 5 hours ago |
| Each time a discussion about sorting starts, I'm reminded of a "lively debate" I had with my boss/friend about the most optimal sorting approach. He claimed it's O(n) pointing to counting sort as an example. This didn't sit well with me. A sorting algorithm, I insisted, should be defined something like "a function taking an unsorted array of elements and returning a sorted one". But it seems there is no agreed upon definition and you can have something like counting sort where you get an array in O(n) but still need O(m) to get the index of a specific element. I argued that then the best sorting algorithm is the do-nothing algorithm. It returns the initial array in O(1), but needd O(m log m) to get you the index of an element (it uses merge sort to do it). |
|
| ▲ | Epa095 4 hours ago | parent | next [-] |
| Usually when one talk about sorting, without specifying closer, one means comparison sort [1], which indeed has an average-case lower bound of O(n*log(n)). In more special cases all kinds of other runtimes are possible. 1: https://en.wikipedia.org/wiki/Comparison_sort |
| |
|
| ▲ | NooneAtAll3 5 hours ago | parent | prev | next [-] |
| counting sort is O(nW), where W is largest value if you don't care about W or it is essentially constant - then it can be dropped but it is an input parameter that will change execution time |
| |
| ▲ | marcosdumay 3 hours ago | parent | next [-] | | It's O(n+W), not O(n*W). > if you don't care about W or it is essentially constant - then it can be dropped Also, every algorithm that ends in a real computer is bound to a constant time.
That's still not a practical thing to do. | |
| ▲ | emil-lp 4 hours ago | parent | prev [-] | | W is the span or range. |
|
|
| ▲ | 4 hours ago | parent | prev | next [-] |
| [deleted] |
|
| ▲ | hinkley 4 hours ago | parent | prev | next [-] |
| I can’t think of a single time I’ve needed a sorted list of only numbers. It’s always numbers and something else, like names or dates. Maybe for median calculations, but I don’t even use those that much either. Especially in telemetry, where mean is easy and median is not. |
| |
| ▲ | pvillano 4 hours ago | parent | next [-] | | To be pedantic, median is cheaper than sorting. O(n) with a quicksort-like algorithm. Also, if you're taking an average of floating point numbers, you might want to sort it first and add from smallest to largest, to better preserve precision | | |
| ▲ | lscharen 3 hours ago | parent [-] | | An aside, but I recently learned -- if one is willing to use a very modest amount of memory -- summing floating-point numbers with no loss of precision is effectively a solved problem with the XSUM algorithm. https://glizen.com/radfordneal/ftp/xsum.pdf | | |
| ▲ | tialaramex 2 hours ago | parent [-] | | That paper explains some useful optimisation details, but obviously since the floats are all (either infinity or) some multiple of a known tiny fraction (their smallest non-zero number), we can definitely sum them accurately. |
|
| |
| ▲ | vlovich123 4 hours ago | parent | prev [-] | | If the primary key is the number, it still works (and dates are just numbers by the way) because you can sort a heterogenous dataset by a single numeric key pretty trivially. But sorting by arbitrary strings like names can’t avoid comparison sort. |
|
|
| ▲ | myhf 4 hours ago | parent | prev | next [-] |
| "ordering" means arranging things in order by some metric. "sorting" means assigning things into bins (which are usually ordered). |
| |
| ▲ | emil-lp 4 hours ago | parent [-] | | This is news to me. Source? | | |
| ▲ | nh23423fefe 2 hours ago | parent | next [-] | | "The children were sorted in to two lines by gender then ordered by height" You might substitute "sorted by height" but its certainly not a correction. While "ordered into lines" would be an error. | |
| ▲ | ninalanyon an hour ago | parent | prev | next [-] | | The sorting office for a postal service. | |
| ▲ | thom 2 hours ago | parent | prev [-] | | What do you do when you sort your washing? |
|
|
|
| ▲ | tpoacher 5 hours ago | parent | prev [-] |
| You reminded me of "Sleep Sort" [0] [0] https://news.ycombinator.com/item?id=2657277 |
| |
| ▲ | pelcg an hour ago | parent [-] | | Don't know about if Sleep Sort even is a valid sorting algorithm? Is this even real? |
|