Question

Implement GenericMergeSort public static<E extends Comparable<E>> void  MergeSort(E[] list) { //implement the body }

Implement GenericMergeSort

public static<E extends Comparable<E>> void  MergeSort(E[] list) {

//implement the body

}

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

public static <E extends Comparable<E>> void mergeSort(E[] list) {

      if (list.length > 1) {

            // Merge sort the first half

            E[] firstHalf = (E[])(new Comparable[list.length / 2]);

            System.arraycopy(list, 0, firstHalf, 0, list.length / 2);

            mergeSort(firstHalf);

            // Merge sort the second half

            int secondHalfLength = list.length - list.length / 2;

            E[] secondHalf = (E[])(new Comparable[secondHalfLength]);

            System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);

            mergeSort(secondHalf);

            // Merge firstHalf with secondHalf into list

            merge(firstHalf, secondHalf, list);

      }

}

public static <E extends Comparable<E>> void merge(E[] list1, E[] list2, E[] temp) {

      int current1 = 0; // Current index in list1

      int current2 = 0; // Current index in list2

      int current3 = 0; // Current index in temp

      while (current1 < list1.length && current2 < list2.length) {

            if (list1[current1].compareTo(list2[current2]) < 0)

                  temp[current3++] = list1[current1++];

            else

                  temp[current3++] = list2[current2++];

      }

      while (current1 < list1.length)

            temp[current3++] = list1[current1++];

      while (current2 < list2.length)

            temp[current3++] = list2[current2++];

}

Add a comment
Know the answer?
Add Answer to:
Implement GenericMergeSort public static<E extends Comparable<E>> void  MergeSort(E[] list) { //implement the body }
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
  • 1. Implement generic insertion sort public static <E extends Comparable<E>> void insertionSort(E[] list){ //implement body }...

    1. Implement generic insertion sort public static <E extends Comparable<E>> void insertionSort(E[] list){ //implement body } 2. Implement generic bubble sort public static <E extends Comparable<E>> void bubbleSort(E[] list){ //implement body } 3. Implement generic merge sort public static <E extends Comparable<E>> void mergeSort(E[] list){ //implement body } 4. Implement generic heap sort public static <E extends Comparable<E>> void heapSort(E[] list){ //implement body }

  • public class myLinkedList<E extends Comparable<E>>{       private Node<E> head = null;                    private Node<E>...

    public class myLinkedList<E extends Comparable<E>>{       private Node<E> head = null;                    private Node<E> tail = null;                       public myLinkedList() {        head = null;       } public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList       }          public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element         ...

  • public class Polymorph public static class CAL public void printHello System.out.println("Hya"); public static class w extends...

    public class Polymorph public static class CAL public void printHello System.out.println("Hya"); public static class w extends CAC public void printHello System.out.println("Howdy"); public static class PB extends WE public void printtello System.out.println("Hello") public static void main(String[] args) { A l = new CÁO: CA ab - (CA) new W(); CA ac - (CA) new PC); Wbc - (W) new P80; 33.printHello(); ab.printHello(); ac.printHello(); be.printHello(); Type your answer, do not copy/paste

  • Implement the following operation for doubly Linked List: public void insertAt(int position, E data){ } public...

    Implement the following operation for doubly Linked List: public void insertAt(int position, E data){ } public void insertEnd(E data){ } public void deleteAt(int position) { } public void deleteEnd(){ }

  • private static void sort(Comparable[] a, int lo, int hi) { if (hi <= lo) return; int...

    private static void sort(Comparable[] a, int lo, int hi) { if (hi <= lo) return; int j = partition(a, lo, hi); show(a, j, lo, hi); sort(a, lo, j-1); sort(a, j+1, hi); assert issorted(a, lo, hi); } Give the code fragment above, what type of sort is this? Insertion sort Bubble sort Quicksort Mergesort

  • Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends...

    Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends Comparable<T>> { /** * Adds a new entry to this priority queue * @param newEntry An object to be added. */ void add(T newEntry); /** Removes and returns the entry having the highest priority. * @return Either the object having the highest priority or, if the priority * queue is empty before the operation, null. */ T remove(); /** Retrieves the entry having the...

  • I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on)...

    I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...

  • Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args)...

    Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args) {         TestThread thread = new TestThread();     }     @Override     public void run() {         printMyName();     }     private void printMyName() {         System.out.println("Thread is running");     } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...

  • class x { x () {} private void one () {} } public class Y extends...

    class x { x () {} private void one () {} } public class Y extends x { Y() {} private void two () { one(); } public static void main (String[] args) { new Y().two(); } } What changes will make this code compile? a. Adding the public modifier to the declaration of class x b. Adding the protected modifier to the x() constructor c. Changing the private modifier on the declaration of the one() method to protected d....

  • public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private...

    public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count;   } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...

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