MergeSort Algorithm for Sorting Arrays - Step by Step Guide

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    comp sci
AI Thread Summary
The discussion focuses on implementing the MergeSort algorithm for sorting arrays, emphasizing the importance of the merging step after sorting the two halves. The initial steps involve checking if the array has one element, splitting it into two halves, and sorting each half. The challenge lies in efficiently merging the two sorted halves, leveraging their sorted nature for optimal performance. Participants suggest prioritizing the merging algorithm to enhance efficiency. Overall, the conversation highlights the recursive nature of MergeSort and encourages exploring additional resources for coding examples.
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 ?
 
TL;DR Summary: I came across this question from a Sri Lankan A-level textbook. Question - An ice cube with a length of 10 cm is immersed in water at 0 °C. An observer observes the ice cube from the water, and it seems to be 7.75 cm long. If the refractive index of water is 4/3, find the height of the ice cube immersed in the water. I could not understand how the apparent height of the ice cube in the water depends on the height of the ice cube immersed in the water. Does anyone have an...
Thread 'Variable mass system : water sprayed into a moving container'
Starting with the mass considerations #m(t)# is mass of water #M_{c}# mass of container and #M(t)# mass of total system $$M(t) = M_{C} + m(t)$$ $$\Rightarrow \frac{dM(t)}{dt} = \frac{dm(t)}{dt}$$ $$P_i = Mv + u \, dm$$ $$P_f = (M + dm)(v + dv)$$ $$\Delta P = M \, dv + (v - u) \, dm$$ $$F = \frac{dP}{dt} = M \frac{dv}{dt} + (v - u) \frac{dm}{dt}$$ $$F = u \frac{dm}{dt} = \rho A u^2$$ from conservation of momentum , the cannon recoils with the same force which it applies. $$\quad \frac{dm}{dt}...

Similar threads

Back
Top