Sergey Piterman
1 min readNov 27, 2018

--

Hey! Not a stupid question, though I do want to make sure I’m understanding it correctly.

This approach uses a single heap, but that heap only has K elements in it (one for each array).

In total you have NK elements (K arrays with an average of N elements per array).

You could toss all NK elements into a single heap and then do what you’re talking about, which is essentially heapsort. But that takes longer to run because there are more elements in the heap you need to work through. And that’s what I was talking about when I said you could just concatenate all the arrays and sort them that way.

In this approach you are using a smaller heap, and that cuts down on your time complexity a fair bit:

from NK log(NK) to NK log(K)

Make sense?

--

--

Sergey Piterman
Sergey Piterman

Written by Sergey Piterman

Technical Solutions Consultant @Google. Software Engineer @Outco. Content Creator. Youtube @ bit.ly/sergey-youtube. IG: @sergey.piterman. Linkedin: @spiterman

Responses (1)