Question

Write A JAVA mergesort method that mergesorts array elements in descending order. Write A JAVA binary...

Write A JAVA mergesort method that mergesorts array elements in descending order.

Write A JAVA binary search method that searches x in a sorted array with n elements and returns

its position if exist, -1 otherwise.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public static void MergeSort(int arr[], int left, int right)
{
    if (left < right)
    {
        int mid = (left+right)/2;

        MergeSort(arr, left, mid);
        MergeSort(arr , mid+1, right);

        merge(arr, left, mid, right);
    }
}

public static void merge(int arr[], int left, int mid, int right)
{
    int size1 = mid - left + 1;
    int size2 = right - mid;

    int leftArr[] = new int [size1];
    int rightArr[] = new int [size2];

    for (int i=0; i<size1; ++i)
        leftArr[i] = arr[left + i];
    for (int j=0; j<size2; ++j)
        rightArr[j] = arr[mid + 1+ j];


    int i = 0, j = 0;

    int k = left;
    while (i < size1 && j < size2)
    {
        if (leftArr[i] >= rightArr[j])
        {
            arr[k] = leftArr[i];
            i++;
        }
        else
        {
            arr[k] = rightArr[j];
            j++;
        }
        k++;
    }

    while (i < size1)
    {
        arr[k] = leftArr[i];
        i++;
        k++;
    }

    while (j < size2)
    {
        arr[k] = rightArr[j];
        j++;
        k++;
    }
}

///////////////////////////////////////////////////////////////////////////////////////


public static int binarySearch(int[] array, int num) {
    int low = 0;
    int high = array.length - 1;
    int mid;

    while (high >= low) {
        mid = (low + high) / 2;
        if (num < array[mid])
            high = mid - 1;
        else
        if (num == array[mid])
            return mid;
        else
            low = mid + 1;
    }
    return - 1;
}
Add a comment
Know the answer?
Add Answer to:
Write A JAVA mergesort method that mergesorts array elements in descending order. Write A JAVA binary...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Write in Java An array is sorted (in ascending order) if each element of the...

    Please Write in Java An array is sorted (in ascending order) if each element of the array is less than or equal to the next element . An array of size 0 or 1 is sorted Compare the first two elements of the array ; if they are out of order, the array is not sorted; otherwise, check the if the rest of the array is sorted. Write a boolean -valued  method  named  isSorted that accepts an integer  array , and the number of...

  • Given a sorted (in ascending order) integer array nums of n elements and a target value,...

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a method to perform a binary search for a target value in nums. If target exists, then return its index, otherwise return -1. Analyze the running time, T(n). public int search(int[] nums, int target) { // YOUR CODE GOES HERE } // end search

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • Write an implementation of the following Java method: /* search(ArrayList, int key) -> int method is...

    Write an implementation of the following Java method: /* search(ArrayList, int key) -> int method is passed an array list of integers and an integer key method searches for the key in the array list and returns: the index of the key if it is in the array list -1 otherwise */

  • In Java, write your own methods to do the following: LinearSearch BinarySearch SelectionSort MergeSort InsertionSort BubbleSort...

    In Java, write your own methods to do the following: LinearSearch BinarySearch SelectionSort MergeSort InsertionSort BubbleSort On the given array with the following elements ; Array : 87, 39, 3, 5 ,9 ,7 ,27,1 , 8 ,6 (use this Array as data for all methods except the BinarySearch method). All your methods will be in a class. Write a tester program that will call each of the listed methods from the class above using the given Array as data. For...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

  • (Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array....

    (Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array. The method should receive the search key, starting index and ending index as arguments. If the search key is found, return its index in the array. If the search key is not found, return –1. (NOTE: Complete the recursiveBinarySearch method in the BinaryArray class). java

  • Given a matrix of size NxM sorted in descending order and value V, write a function...

    Given a matrix of size NxM sorted in descending order and value V, write a function which returns -1 if value V is not in the matrix, else it returns 1 if V is in the matrix. The function also computes the position of V in the matrix, that is P= [R, C], where R is the row number of V and C is the column number of V in the matrix, if V is in the matrix (if V...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT