Merge and MergeSort Algorithms for Sorting Arrays in Java

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Program Testing
AI Thread Summary
The discussion focuses on implementing Merge and MergeSort algorithms in Java for sorting arrays. The provided code outlines two classes: Merge, which contains the merging logic, and MergeSort, which handles the recursive sorting process. A user inquires about how to print the sorted array after implementing these classes. The response suggests using a for loop with System.out.println() to display each element of the array. This straightforward approach resolves the user's query about outputting the sorted array.
courtrigrad
Messages
1,236
Reaction score
2
Hello all

Let's say we have the following classes:

Code:
 public class Merge {
protected static void merge(int[] a, int first, int last, int middle) {

        int i, j, k;
        int[] tempArray = new int[a.length];
        i = first;
        j = first;
        k = middle + 1;

        while(j <= middle && k <= last){
            if(a[j] <= a[k])
                tempArray[i++] = a[j++];
            else
                tempArray[i++] = a[k++];
        }
        while(j <= middle)
            tempArray[i++] = a[j++];
        while(k <= last)
            tempArray[i++] = a[k++];

        for (i = first; i <= last; i++)
            a[i] = tempArray[i];
        }
    }

and

Code:
 public class MergeSort extends Merge {
public static void mergeSort(int[] a, int first, int last) {

        if(last==first) return;
        if (last - first == 1) {
            //  not a[0]>a[1]; this part could test on the upper half of the array
            if (a[first] > a[last]) {
                int temp = a[first];
                a[first] = a[last];
                a[last] = temp;
            }
            

        } else {
            int halfwayPoint = (int) (last + first) / 2;
            mergeSort(a, first, halfwayPoint);
            mergeSort(a, halfwayPoint + 1, last);
            merge(a, first, last, halfwayPoint);
           
        }
    }
}

How would I print the array? I am stuck. I wrote the two classes but don't know how to get the output. Do I need a for loop?

Thanks :smile:
 
Physics news on Phys.org
Yeah, just use a for loop to go through each element in turn and print using System.out.println():

Code:
for(int i = 0; i < array.length(); i++)
[indent]System.out.println(array[i]);[/indent]

HTH.
 
thanks a bunch :smile:
 
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 'Calculation of Tensile Forces in Piston-Type Water-Lifting Devices at Elevated Locations'
Figure 1 Overall Structure Diagram Figure 2: Top view of the piston when it is cylindrical A circular opening is created at a height of 5 meters above the water surface. Inside this opening is a sleeve-type piston with a cross-sectional area of 1 square meter. The piston is pulled to the right at a constant speed. The pulling force is(Figure 2): F = ρshg = 1000 × 1 × 5 × 10 = 50,000 N. Figure 3: Modifying the structure to incorporate a fixed internal piston When I modify the piston...
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