i need a simple implementation of sorting algorithm in linear time and efficient ? like radix sort or counting sort ? are they really better than merge sort ..if so why people always use nlogn algorithms ?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
i need a simple implementation of sorting algorithm in linear time and efficient ? like radix sort or counting sort ? are they really better than merge sort ..if so why people always use nlogn algorithms ?
Name |
---|
There can't exist a sorting the runs in a linear time, because you need to make comparisons whether directly like merge sort, quick sort, bubble sort, ... etc or indirectly like radix sort.
how can you sort items when you don't compare them to their surroundings? and if you compare how can the complexity of such algorithm be linear?!
i mean classification sorting algorithms like radix sort or counting sort they run in linear time !
Radix Sort is linear only because it is non-comparative, and Counting Sort takes expected linear time if you use a hashtable (it's not guaranteed linear, it's just linear with high probability).
In general, you cannot do better than if you're using a comparative sorting algorithm. If you have a well-defined ordering on some objects, then you can sort them in assuming you can compare them in O(1).
If you take advantage of some underlying structure within those objects, then yes, you could sort faster than . That's where Radix Sort comes from. It solves the problem of sorting integers specifically, not the problem of sorting objects with an ordering.
It's less effort to just use
std::sort
and be okay with a factor. It's not wrong to use Radix Sort for integers, it's just that in a contest, there's a tradeoff between how well it would benefit your solution, and the time you need to implement it (also, the maintenance cost, and the probability that your implementation has a bug play a role).Radix sort isn't really linear though, but rather where M is the largest number, which isn't that much better/different in theory from since we often have M > N.
(In practice we can make radix sort quite efficient though by processing x-bit chunks at the time.)