Modify the merge sort algorithm so that, instead of sorting an array, it rearranges places all even elements before the odd ones. For example, the array [1, 4, 9, 16, 25] becomes [4, 16, 1, 9, 25]. Use the same recursion where an array is split in two, each half is rearranged, and the two halves are merged together. However, the merge method needs to be changed to merge two arrays, each of which has the even elements before the odd ones, into one larger array with the same property. MergeSorter.java 1 public class MergeSorter 2 { 3 private int[] a; 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 /** */ public MergeSorter (int[] anArray) { } Constructs a merge sorter. @param anArray the array to sort a = anArray; */ public void evenFirst() { } Places the even elements before the odd ones. } /** if (a.length <= 1) return; int[] first = new int [a. length / 2]; int[] second = new int[a.length - first.length]; // Copy the first half of a into first, the second half into second for (int i = 0; i < first.length; i++) { first[i] = a[i]; } for (int i = 0; i < second. length; i++) { second [i] = a[first.length + i]; } MergeSorter first Sorter = new MergeSorter (first); MergeSorter secondSorter = new MergeSorter (second); firstSorter.evenFirst(); secondSorter.evenFirst(); merge (first, second); Merges two arrays into the array managed by this merge sorter. @param first the first array which has the even elements before the odd ones @param second the second array which has the even elements before the odd ones */ private void merge(int[] first, int[] second) { } // this method is used to check your work public static int [] check(int [] a) { MergeSorter sorter = new MergeSorter (a); sorter.evenFirst(); return a; // TODO

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

JAVA

Modify the merge sort algorithm so that, instead of sorting an array, it rearranges places all even elements before the odd ones. For example, the array \([1, 4, 9, 16, 25]\) becomes \([4, 16, 1, 9, 25]\). 

Use the same recursion where an array is split in two, each half is rearranged, and the two halves are merged together. However, the `merge` method needs to be changed to merge two arrays, each of which has the even elements before the odd ones, into one larger array with the same property.

---

**MergeSorter.java**

```java
1  public class MergeSorter
2  {
3     private int[] a;
4 
5     /**
6      Constructs a merge sorter.
7      @param anArray the array to sort
8      */ 
9     public MergeSorter(int[] anArray)
10    {
11       a = anArray;
12    }
13 
14    /**
15     Places the even elements before the odd ones.
16     */
17    public void evenFirst()
18    {
19       if (a.length <= 1) return;
20       int[] first = new int[a.length / 2];
21       int[] second = new int[a.length - first.length];
22       // Copy the first half of a into first, the second half into second
23       for (int i = 0; i < first.length; i++) { first[i] = a[i]; }
24       for (int i = 0; i < second.length; i++)
25       {
26          second[i] = a[first.length + i];
27       }
28       MergeSorter firstSorter = new MergeSorter(first);
29       MergeSorter secondSorter = new MergeSorter(second);
30       firstSorter.evenFirst();
31       secondSorter.evenFirst();
32       merge(first, second);
33    }
34 
35    /**
36     Merges two arrays into the array managed by this
37     merge sorter.
38     @param first the first array which has the even elements before the odd ones
39     @param second the second array which has the even elements before the odd ones
40     */
41    private void merge(int[] first, int[] second)
42    {
43       // TODO
44    }
45 
46    // this method is used to
Transcribed Image Text:Modify the merge sort algorithm so that, instead of sorting an array, it rearranges places all even elements before the odd ones. For example, the array \([1, 4, 9, 16, 25]\) becomes \([4, 16, 1, 9, 25]\). Use the same recursion where an array is split in two, each half is rearranged, and the two halves are merged together. However, the `merge` method needs to be changed to merge two arrays, each of which has the even elements before the odd ones, into one larger array with the same property. --- **MergeSorter.java** ```java 1 public class MergeSorter 2 { 3 private int[] a; 4 5 /** 6 Constructs a merge sorter. 7 @param anArray the array to sort 8 */ 9 public MergeSorter(int[] anArray) 10 { 11 a = anArray; 12 } 13 14 /** 15 Places the even elements before the odd ones. 16 */ 17 public void evenFirst() 18 { 19 if (a.length <= 1) return; 20 int[] first = new int[a.length / 2]; 21 int[] second = new int[a.length - first.length]; 22 // Copy the first half of a into first, the second half into second 23 for (int i = 0; i < first.length; i++) { first[i] = a[i]; } 24 for (int i = 0; i < second.length; i++) 25 { 26 second[i] = a[first.length + i]; 27 } 28 MergeSorter firstSorter = new MergeSorter(first); 29 MergeSorter secondSorter = new MergeSorter(second); 30 firstSorter.evenFirst(); 31 secondSorter.evenFirst(); 32 merge(first, second); 33 } 34 35 /** 36 Merges two arrays into the array managed by this 37 merge sorter. 38 @param first the first array which has the even elements before the odd ones 39 @param second the second array which has the even elements before the odd ones 40 */ 41 private void merge(int[] first, int[] second) 42 { 43 // TODO 44 } 45 46 // this method is used to
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY