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 ?
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...

Similar threads

Back
Top