Merge and MergeSort Algorithms for Sorting Arrays in Java

In summary, the conversation discusses the implementation of two classes, Merge and MergeSort, for sorting an array of integers using the merge sort algorithm. The code for the classes is provided and the question of how to print the sorted array is raised. It is suggested to use a for loop to iterate through the elements of the array and print them using System.out.println().
  • #1
courtrigrad
1,236
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
  • #2
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.
 
  • #3
thanks a bunch :smile:
 

FAQ: Merge and MergeSort Algorithms for Sorting Arrays in Java

What is a merge algorithm and how does it work?

A merge algorithm is a method used to combine two or more sorted arrays into a single sorted array. It works by comparing the elements of the two arrays and placing them in the correct order in the new array. This process continues until all the elements from both arrays are included in the new sorted array.

What is the time complexity of a merge algorithm?

The time complexity of a merge algorithm is O(n), where n is the total number of elements in the arrays being merged. This is because the algorithm only needs to compare each element once and place it in the correct position in the new array.

What is a mergesort algorithm and how does it differ from a regular merge algorithm?

A mergesort algorithm is a sorting algorithm that uses the merge algorithm to sort an array. It works by dividing the array into smaller subarrays, sorting them, and then merging them back together. This differs from a regular merge algorithm because it can handle larger arrays and is more efficient for sorting larger datasets.

What is the time complexity of a mergesort algorithm?

The time complexity of a mergesort algorithm is O(n log n), where n is the total number of elements in the array. This is because the algorithm divides the array into smaller subarrays, sorts them, and then merges them back together in a logarithmic time complexity.

Can merge and mergesort algorithms be used for sorting any type of data?

Yes, merge and mergesort algorithms can be used for sorting any type of data as long as the data can be compared and placed in a specific order. This includes numbers, strings, and other data types in Java.

Similar threads

Replies
7
Views
2K
Replies
3
Views
1K
Replies
25
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Back
Top