Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function that takes an array Arr of size N and returns another array whi

ID: 3778636 • Letter: W

Question

Write a function that takes an array Arr of size N and returns another array which, first disregards the numbers greater than a certain threshold (also a passed parameter), and then stores the remaining elements of the array Arr first in an increasing order and then in a decreasing order

Example: If Arr = {5,4,1,12,0,2} and threshold = 10, the returned array should be = {0,1,2,4,5,5,4,2,1,0}

In the main method, the user is asked to enter the size of array Arr, the value of the threshold, as well as all the elements of array Arr.

Notes and hints:

Write an iterative merge sort method to do the sorting part.

You may need to define additional functions.

The size of the returned array is dynamic (depending on the number of eliminated elements). Be careful how you return such an array.

Explanation / Answer

Following is the code that takes an array Arr of size N and returns another array which, first disregards the numbers greater than a certain threshold and uses mergesort to do the sorting part.

class MSort

{

    void merge(int arr[], int a, int b, int c, int t)

    {

        // Find sizes of two sub arrays to be merged with the given threshold

        int n1 = b - a + 1;

        int n2 = c - b;

      

        /* Creating temp arrays where data is stored*/

        int A[] = new int [n1];

        int C[] = new int [n2];

        /*Copying data to temp arrays*/

        for (int i=0; i<n1; ++i)

            A[i] = arr[l + i];

        for (int j=0; j<n2; ++j)

            C[j] = arr[m + 1+ j];

        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays

        int i = 0, j = 0;

        // Initial index of merged subarray

        int k = l;

        while (i < n1 && j < n2)

        {

            if (A[i] < C[j])

            {

                arr[k] = A[i];

                i++;

            }

            else

            {

                arr[k] = C[j];

                j++;

            }

            k++;

        }

        /* Copying remaining elements of L[] if any */

        while (i < n1)

        {

            arr[k] = A[i];

            i++;

            k++;

        }

        /* Copying remaining elements of A[] if any */

        while (j < n2)

        {

            arr[k] = C[j];

            j++;

            k++;

        }

    }

    // Main function that sorts arr[l..r] using

    // merge()

    void sort(int arr[], int a, int c)

    {

        if (a < c)

        {

            // Finding the middle point and sorting both the halves

            int b = (a+c)/2;

           

            sort(arr, a, b);

            sort(arr , b+1, c);

      

            merge(arr, l, b, c);

        }

    }

    /* A function to print array of size n */

    static void printArray(int arr[])

    {

        int n = arr.len;

        for (int i=0; i<n; ++i)

            System.out.print(arr[i] + " ");

        System.out.println();

    }

    // Given is the driver method

    public static void main(String args[])

    {

        int arr[] = {12, 11, 13, 5, 6, 7};

        System.out.println("Given Array");

        printArray(arr);

        MSort ob = new MSort();

        ob.sort(arr, 0, arr.len-1);

        System.out.println(" Sorted array");

        printArray(arr);

    }

}

/*To print the array in reverse*/

class RArray {

    /* Function to reverse arr[] from start to end*/

    static void revArr(int arr[], int start1, int end1)

    {

        int temp1;

        if (start1 >= end1)

            return;

        temp1 = arr[start1];

        arr[start1] = arr[end1];

        arr[end1] = temp1;

        revArray(arr, start1+1, end1-1);

    }

    /* print out an array on a line */

    static void printArr(int arr[], int size1)

    {

        int i;

        for (i=0; i < size1; i++)

            System.out.println(arr[i] + " ");

        

    }

  

    public static void main (String[] args)

{

        int arr[] = {5, 6, 7, 11, 12, 13};

        printArray(arr, 6);

        revArray(arr, 0, 5);

        System.out.println("Reversed array is ");

        printArray(arr, 6);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote