What is the purpose of the MERGESORT algorithm?

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary

Discussion Overview

The discussion revolves around the MERGESORT algorithm, focusing on its purpose, functionality, and the process of sorting an array. Participants explore the algorithm's structure, the associated MERGE function, and how to apply it to specific examples.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Exploratory

Main Points Raised

  • Some participants describe MERGESORT as an algorithm that splits an array into two halves, sorts each half, and then merges the sorted halves.
  • Others inquire about the MERGE function, suggesting it is necessary for understanding how the sorting occurs.
  • A participant proposes an example array (3,5,2,7) and anticipates that the sorted result will be (2,3,5,7).
  • Some participants express confusion regarding the merging process and seek clarification on how two sorted arrays are combined into a single sorted array.
  • One participant illustrates the sorting of a different array (4,7,5,9,1,3,2,8) and explains the merging of the sorted subarrays step by step.

Areas of Agreement / Disagreement

Participants generally agree on the basic functionality of MERGESORT, but there is uncertainty regarding the details of the merging process and how the algorithm operates in practice. Some participants express confusion, indicating that the discussion remains unresolved in terms of fully understanding the algorithm's mechanics.

Contextual Notes

There are unresolved questions about the implementation details of the MERGE function and how it integrates with the MERGESORT process. Participants have not reached a consensus on the clarity of the merging steps.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Mmm)

I am looking at the following algorithm:

Code:
MERGESORT(A,l,r){
   if (R==L) return;
   MERGESORT(A,l,floor((r+l)/2));
   MERGESORT(A,floor((r+l)/2)+1,r);
   return MERGE(A,l,r,floor((r+l)/2));

What does the above algorithm do? Could you explain it to me? (Thinking)
 
Technology news on Phys.org
evinda said:
Hello! (Mmm)

I am looking at the following algorithm:

Code:
MERGESORT(A,l,r){
   if (R==L) return;
   MERGESORT(A,l,floor((r+l)/2));
   MERGESORT(A,floor((r+l)/2)+1,r);
   return MERGE(A,l,r,floor((r+l)/2));

What does the above algorithm do? Could you explain it to me? (Thinking)

Hey! (Wave)

This merge-sort algorithm splits an array into 2 halves.
Then it "merge-sorts" each half.
The resultant array is "merged".

The same procedure is apply to any of those parts. (Nerd)
 
I like Serena said:
Hey! (Wave)

This merge-sort algorithm splits an array into 2 halves.
Then it "merge-sorts" each half.
The resultant array is "merged".

The same procedure is apply to any of those parts. (Nerd)

Could you give me an example, in order to apply the algorithm? (Wasntme)
 
evinda said:
Could you give me an example, in order to apply the algorithm? (Wasntme)

Well, first you need the algorithm for MERGE.
Can you tell us what it is? (Wondering)

And then we should try to apply it to for instance (3,5,2,7)... (Thinking)
I can already tell you that (2,3,5,7) will come out! (Smile)
 
I like Serena said:
Well, first you need the algorithm for MERGE.
Can you tell us what it is? (Wondering)
This is the algorithm [m] MERGE [/m] :

Code:
MERGE(A,p,q,r)
   n1=q-p+1
   n2=r-q;
   we create the arrays L[1 .. n1+1] and R[1 .. n2+1]
   for i=1 till n1
        L[i]=A[p+i-1]
   for j=1 till n2
        R[j]=A[q+j]
   L[n1+1]=oo
   R[n2+1]=oo
   i=1
   j=1
   for k=p till r
        if (L[i]<=R[j])
           then A[k]=L[i]
                  i=i+1
           else A[k]=R[j]
                 j=j+1
I like Serena said:
And then we should try to apply it to for instance (3,5,2,7)... (Thinking)
I can already tell you that (2,3,5,7) will come out! (Smile)

Before executing the command [m] return MERGE(A,l,r,floor((r+l)/2)); [/m], the following commands are executed:

[m] MERGESORT(A,0,1) [/m]

[m] MERGESORT(A,0,0) [/m]

[m] MERGESORT(A,2,3) [/m]

[m] MERGESORT(A,2,2) [/m]

[m] MERGESORT(A,3,3) [/m]Right? (Thinking)
What do we get from these calls? (Callme) (Thinking)
 
evinda said:
This is the algorithm [m] MERGE [/m] :

That's quite a complicated algorithm! (Sweating)

Well, it may suffice that it merges 2 sorted arrays into a new array, that is then sorted. (Whew)

Before executing the command [m] return MERGE(A,l,r,floor((r+l)/2)); [/m], the following commands are executed:

Right? (Thinking)
What do we get from these calls? (Callme) (Thinking)

Well, [m] MERGESORT(A,0,0) [/m] returns without doing anything.
Just like [m] MERGESORT(A,1,1) [/m], [m] MERGESORT(A,2,2) [/m], and [m] MERGESORT(A,3,3) [/m].

And after those, MERGE will be called. (Thinking)
 
I like Serena said:
That's quite a complicated algorithm! (Sweating)

Well, it may suffice that it merges 2 sorted arrays into a new array, that is then sorted. (Whew)
Well, [m] MERGESORT(A,0,0) [/m] returns without doing anything.
Just like [m] MERGESORT(A,1,1) [/m], [m] MERGESORT(A,2,2) [/m], and [m] MERGESORT(A,3,3) [/m].

And after those, MERGE will be called. (Thinking)

What is then the function of [m] MERGESORT [/m] ? (Worried)
I haven't understood how we merge like that two sorted arrays? :confused:
 
evinda said:
What is then the function of [m] MERGESORT [/m] ? (Worried)
I haven't understood how we merge like that two sorted arrays? :confused:

Suppose we have the array L=(4,7,5,9,1,3,2,8).
Then first we sort L1=(4,7,5,9) and L2=(1,3,2,8).
Without going into the details, that will come out as S1=(4,5,7,9) respectively S2=(1,2,3,8).
Then we merge them while keeping the result sorted. (Thinking)

That is, we go through them entry by entry, each time selecting the smallest from both arrays.
First is 1 from S2, followed by 2 and 3 that also come from S2.
That gives us S=(1,2,3).
Then we continue with 4 from S1, so we get S=(1,2,3,4).
And we keep going until we have S=(1,2,3,4,5,7,8,9). (Nerd)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 43 ·
2
Replies
43
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
25
Views
4K