1
$\begingroup$

I want to show that if I apply the Mergesort on a set $M$ with $n$ elements and divide $M$ in $2 My first question, when I am merging, do I merge $b$ subsets together or just two?

So I went with $b$ subsets:
In the first merging part I have $b^k (b-1) $ comparisons.
In the next one I habe $b^{k-1} (b^2-1) $ comparisons.
So in general there are at most $\sum_{i=0}^k b^{k-i} (b^{i+1}-1) $ comparisons.

Now I set $k = log_b(n)$, but I cant get anywhere from there..
Is there another way to approach this Problem?

Regards,
Chiray

P.S.: I hope my English is good enough to understand this.

1 Answers 1

0

If you use a priority queue for the merging phase, you never need to have more than $b$ elements in the queue. The insertion cost is therefore $O(\log b)$ for common implementations of priority queues.

The observation that leads to the use of a priority queue is simple. After the recursive calls, we have $b$ sorted lists. If we can efficiently keep track of the order of the $b$ elements at the fronts of those lists, we get to merge quickly. A priority queue can be used to store $b$ elements, each tagged with the list it comes from. The smallest element is extracted from the queue. Its tag tells us from which list to get another element. This new element is inserted in the queue.

  • 0
    I don't know what a priority queue is, but that is also not what I want to show. I want to show, that if you use the basic merging algorithm, that you need $O(nlog(b))$ time2017-01-08
  • 0
    What exactly is the "basic merging algorithm" for you? There are certainly ways to merge that cost more than $O(n \log b)$. If you do $b$ comparisons for each element you insert in the final sorted sequence, then obviously the cost of merging is $O(nb)$.2017-01-08
  • 0
    I compare the first elements of the subsets, and remove the lowest element.2017-01-08
  • 0
    I continue doing this til one subset is empty2017-01-08
  • 0
    Do you know that I mean?2017-01-08
  • 0
    Yes, that looks $O(bn)$ to me. I'll be out of touch for a while, but I'd like you to think of this: to get to $O(n \log b)$, you have to get the most out of each comparison. Do not compare the same pair of element more than once.2017-01-08
  • 0
    ok thank you, I will try2017-01-08
  • 0
    lets say $b=n$ then I merge only one time. And I have $n-1 + n-2 + ... + 1$ comparisons, so $O(n^2)$ isnt it?2017-01-08
  • 0
    So I did some research and dividing into more than two parts is called Polyphase merge sort. And they use the priority queue. So I will read up on that and see where I can get from there.2017-01-08
  • 0
    @Chiray Yes, if $b=n$, that algorithm will be quadratic, and yes, the use of a priority queue is standard in this application.2017-01-08
  • 0
    Do you know how to proof the $O(nlog(b))$ time?2017-01-08
  • 0
    If every one of the $n$ numbers requires $O(\log b)$ comparisons and $O(1)$ time for other operations, then the whole process can be completed in $O(n \log b)$ time.2017-01-08
  • 0
    how dod I prove that each numbers requieres $O(logb)$ ?2017-01-08
  • 0
    That depends on which priority queue you use, but for common implementations like heaps or various forms of balanced trees, you can count on the proof being already done.2017-01-08
  • 0
    Ok, I don't really understand how the priority queue works in the merging phase..2017-01-08
  • 0
    Is it the way to use the queue or is it the complexity that you don't understand?2017-01-08
  • 0
    both, but how would this queue help in the merging phase? e. g. if b would be 3. And after the second merge you have three subsets with 147.. 258.. 369?2017-01-08
  • 0
    You start by nserting the first element of each of the $b$ sorted lists. These elements are removed from their lists. You extract the min. and put it in the final sorted list. Now you add the element at the front of the list from which the min came to the queue. Extract the new min and continue until all $n$ elements have been moved to the main sorted list.2017-01-08
  • 0
    Isn't this the same to what I said ealier? " I compare the first elements of the subsets, and remove the lowest element."2017-01-08
  • 0
    In part. At the very high level, of course, it's the same. The key point is how to figure out the next lowest element with $O(\log b)$ comparisons. That's were a properly implemented priority queue helps.2017-01-08
  • 0
    I asked today my tutor, and he said the same as you, but I understand it now! Thank you for your help!2017-01-09