Implementing MergeSort with the given method signature

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    comp sci
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
courtrigrad
Messages
1,236
Reaction score
2
Let's say you want to write an algorithm for MergeSort given the following:
Code:
 public void(int[]a, int n1, int m, int n2)
I know the algorithm:


MergeSort
1. If array has 1 element don't don anything
2. Split array in two halves
3. Sort the first half and the second half
4. Merge both halves into one.

Any help is appreciated :smile:
 
Physics news on Phys.org
courtrigrad said:
Let's say you want to write an algorithm for MergeSort given the following:
Code:
 public void(int[]a, int n1, int m, int n2)
I know the algorithm:


MergeSort
1. If array has 1 element don't don anything
2. Split array in two halves
3. Sort the first half and the second half
4. Merge both halves into one.

Any help is appreciated :smile:

The tough part of this problem is number 4. I'd suggest working on this part first... assuming you have an array (whose length can be anything >=2) that has the top half sorted, and the bottom half sorted, write an algorithm that merges the two. You want to make this as efficient as possible. You want to take advantage of the fact that the top and bottom are each sorted. Compared to this part, the rest is easy.

Mergesort is a neat application of recursion.
 
I remember downlopading java gives a lot of free soring examples. why not chekc it out ?