Merge and MergeSort Algorithms for Sorting Arrays in Java

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Program Testing
Click For Summary
SUMMARY

The discussion centers on implementing Merge and MergeSort algorithms in Java for sorting arrays. The provided code includes a Merge class with a static merge method and a MergeSort class that extends Merge, featuring a mergeSort method. The user inquires about printing the sorted array, and the solution involves using a for loop with System.out.println() to display each element. This straightforward approach effectively outputs the sorted array after applying the MergeSort algorithm.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of sorting algorithms, specifically MergeSort
  • Basic knowledge of array manipulation in Java
  • Familiarity with object-oriented programming concepts in Java
NEXT STEPS
  • Implement additional sorting algorithms such as QuickSort and BubbleSort in Java
  • Explore Java Collections Framework for sorting lists
  • Learn about time complexity analysis of sorting algorithms
  • Investigate Java's built-in sorting methods, such as Arrays.sort()
USEFUL FOR

Java developers, computer science students, and anyone interested in understanding sorting algorithms and their implementations in Java.

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:
 

Similar threads

Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K