Knowee
Questions
Features
Study Tools

answer this question in simple code for beginners using intellJ: 4. Write a Java program to sort an array of given integers using the Merge Sort Algorithm.

Question

answer this question in simple code for beginners using intellJ: 4. Write a Java program to sort an array of given integers using the Merge Sort Algorithm.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple implementation of Merge Sort in Java:

public class Main {

    public static void main(String[] args) {
        int[] array = {12, 11, 13, 5, 6, 7};
        System.out.println("Given Array");
        printArray(array);
        mergeSort(array, 0, array.length - 1);
        System.out.println("\nSorted array");
        printArray(array);
    }

    public static void mergeSort(int[] array, int left, int right) {
        if (left < right) {
            int mid = (left + right) / 2;
            mergeSort(array, left, mid);
            mergeSort(array, mid + 1, right);
            merge(array, left, mid, right);
        }
    }

    public static void merge(int[] array, int left, int mid, int right) {
        int n1 = mid - left + 1;
        int n2 = right - mid;

        int[] Left = new int[n1];
        int[] Right = new int[n2];

        for (int i = 0; i < n1; ++i)
            Left[i] = array[left + i];
        for (int j = 0; j < n2; ++j)
            Right[j] = array[mid + 1 + j];

        int i = 0, j = 0;
        int k = left;
        while (i < n1 && j < n2) {
            if (Left[i] <= Right[j]) {
                array[k] = Left[i];
                i++;
            } else {
                array[k] = Right[j];
                j++;
            }
            k++;
        }

        while (i < n1) {
            array[k] = Left[i];
            i++;
            k++;
        }

        while (j < n2) {
            array[k] = Right[j];
            j++;
            k++;
        }
    }

    public static void printArray(int[] array) {
        int n = array.length;
        for (int i = 0; i < n; ++i)
            System.out.print(array[i] + " ");
        System.out.println();
    }
}

This program first prints the given array, sorts it using the merge sort algorithm, and then prints the sorted array. The mergeSort function is a recursive function that splits the array into two halves and sorts them separately. The merge function is used to merge two sorted halves into a sorted whole.

This problem has been solved

Similar Questions

sort a numeric array and a string arrayWrite a Java program to sort a numeric array and a string array.

Develop a Java program for sorting an integer array, floating-point array and character array. Use Generic method for the implementation. Finally print the sorted array. Note: consider the size of the array to be 5.

Design a Java program that allows users to input a series of integers into an array and then sorts the array in ascending order. Implement the sorting algorithm using the bubble sort technique.Write a Java code that accomplishes the following tasks:Prompt the user to enter the number of elements in the array.Prompt the user to input each element of the array.Sort the array in ascending order using the bubble sort algorithm.Display the sorted array.Demonstrate the functionality of your program with a sample input/output scenario.Sample Input12 // No of elements 23 // Elements to store in an array674512900260725434129865Sample Output12 12 23 34 45 54 65 67 72 98 260 900

answer this question in simple code for beginners using intellJ: Sort an integer list, then find the pair that adds up to the given number

Language: Javapublic int[] sort(int[] numbers, int length)if (length < 2) {}return numbers;int mid length / 2;6int[] left = new int[id]:7int[] right new int[length mid]:9for (int i=0; i<mid; i++) {1011}1213left[i] numbers[i];for (int i-mid; i < length; i++) {INSERT MISSING CODE14}151617sort(left, mid);sort(right, length - mid);1819return merge(numbers, left, right, mid, length - mid);20}Image not displaying?Complete the sort function by replacing // INSERT MISSING CODE with the correct line of code.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.