I was thinking that we can create algorithm for sorting that will work faster than $O(N\log N)$

Let's say we have given array $A$ consisting of $N$ integers, where $N = 10^6$. Our task is to sort this given array. The clear solution is to sort the array using the classic merge sort algorithm, which works in $O(N \log N)$ time and space complexity. I was thinking that this trick can improve the complexity.

Let $N_{1} = 10^5$, clearly $N = 10\cdot N_{1}$. Let's split the array $A$ in $10$ smaller array where each array consist of $N_{1}$ elements and sort those $10$ arrays with standard merge sort ( time complexity: $O(N_{1}\cdot\log N_{1})$), so we have 10 arrays where each array is sorted, we can merge those 10 array into one array in time complexity $O(N\cdot\log(10))$ using priority queue.

So the total complexity will be $O(10 \cdot N_{1}\cdot\log N_{1} +N\cdot\log(10))$. Now let's say we split the array in $\sqrt{N}$ arrays, so the complexity will look like: $$O(\sqrt{N} \cdot \sqrt{N} \cdot \log \sqrt N + N \cdot \sqrt N)\\ = O(N \cdot \log (\sqrt N) + N \log (\sqrt N))$$

Is everything in my algorithm correct, and is this trick efficent in memory usage?

  • $\begingroup$ How can you do it with priority queue that fast? Something is wrong as wikipedia says that this is lower bound when using comparisons for sorting. $\endgroup$
    – rus9384
    Commented Aug 26, 2017 at 11:04
  • $\begingroup$ It is not hard to do, we put in priority queue the first element from all arrays, then when we take one element from one array, we put in the queue the next one... we just need to keep index where are we in each array $\endgroup$ Commented Aug 26, 2017 at 11:07
  • $\begingroup$ If your only access to elements is by comparing them and moving them around, then there is an $\Omega(n\log n)$ lower bound. If your elements are integers and you're not restricting yourself, then better algorithms are known. $\endgroup$ Commented Aug 26, 2017 at 11:08
  • $\begingroup$ Note that $O(n\log n)$ and $O(n\log\sqrt n)$ are the same complexity class. $\endgroup$ Commented Aug 26, 2017 at 11:09
  • 2
    $\begingroup$ Count sort is O(n) $\endgroup$
    – paparazzo
    Commented Aug 26, 2017 at 16:59

2 Answers 2


You got a result of $O(N\cdot \log(\sqrt N)+N\log(\sqrt N))$.

But $\log \sqrt N = (\log N)/2$, so $N⋅log(\sqrt N)+N·\log(\sqrt N) = N·\log N$. So not only is it the same complexity class, where constant factors don't matter, it is even the same function, therefore the same constant factor.

But the underlying reason for any comparison based sorting algorithm to take $O(\log N)$ comparisons is that there are N! possible permutations of the array elements, each comparison can rule out at most half the possible cases, so you absolutely need at least $\log_2 N!$ comparisons in the worst case, which is $O(N\cdot \log N)$. That $\log_2 N!$ is not a big-O, it is an absolute number. You cannot achieve for example $0.999 \log_2 N!$ comparisons in the worst case.

So you would have to come up with an algorithm that isn't based on comparison. For example, if an image contains pixels using 24 bits each, then you can sort the $N$ pixels of an image in $O(N)$ (see "The Player of Games" for reasons why you would want to sort all the pixels in a large library).


Your example is an unfortunate one. Since you are sorting integers, you can pose a specific requirement to sort, such as the value, if that is the only thing to sort on you can use the radix sort, which would put the complexity at O(#ofdigitsn) or O(kn) which is 99% of cases is much better than lg(n), the only reason is sorting alrogithm isn't used more often is because it is too restrictive with the type of data it can sort.

For example: If you had 10^6 integers in the worst cause scenario, because integers can only have 8 digits, you would have 8*10^6 calculations for radix sort, but for MergeSort you would have 20*10^6 which is significantly slower.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.