Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi there, I need help writing a short method in my java program. Please find the problem below, as well as the code I have

Hi there, I need help writing a short method in my java program. Please find the problem below, as well as the code I have so far. And please include comments if possible so that I may understand better. Note**: something similar to this link will be helpful: https://www.geeksforgeeks.org/merge-two-sorted-arrays/

Meaning, the implementation should not be the one which divides the arrays into halves..etc.

Will rate!!image text in transcribedimage text in transcribed

// an adaptive, therefore more versatile array public class SmarterArray { // data private int[] data; private int elements; // constructor public SmarterArray() { data = new int[4]; elements = 0; } // double the array's size public void doubleSize() { // create temp array with 2x the size int[] tmp = new int[2 * data.length]; // transfer existing data for(int i = 0; i = data.length) { doubleSize(); } // now add the new piece of data data[elements] = val; elements++; }

// a method to print public void print() { // we only want to print valid data (elements), not the entire holding space (length) for(int i = 0; i data[loc]) { loc = i; } } // when done, return location found return loc; } // get a value, given an index public int get(int loc) { return data[loc]; } // set a value, given an index public void set(int loc, int value) { data[loc] = value; } // find an element // return the index if found // -1 if not found, a valid index is never negative public int find(int value) { // loop through the array, return index if found, -1 if reaching end without finding for(int i = 0; i

public void diySort() { // the maximum int max = data[max()]; // second array (same size) to sort into int[] tmp = new int[data.length]; // transfer all elements for(int i = 0; i

// public void merge()

{

// logic here

} public static void main(String[] args) { SmarterArray smarter = new SmarterArray(); smarter.add(6); smarter.add(-7); smarter.add(9); smarter.add(1); smarter.add(13); smarter.add(16); smarter.add(-77); smarter.add(19); smarter.add(12); smarter.add(-1); smarter.print();

} }

This will not implement a full merge sort, but only the merge step that merges two already sorted arrays Add a method void merge(intll param) to SmartArray. It must merge the sorted parameter array into a sorted SmartArray, maintaining the order in the resulting array. You will provide the parameter array 1. Note that the implementation will require a third array to merge into. We want the behavior as outlined below. Implement the method such that the SmartArray's free space before and after the merge is equal. 'Free space' refers to the difference between data.length and elements, the available storage slots. 2. Test the method in main: called 'smart, say,-7,2, // assume a sorted instance of SmartArray 4, 9, 13 smart.print(); // will print-7, 2, 4,9, 13 smart.merge(new int[l i-9, 1, 6, 221); smart.print); //must print the merged data -9,-7,1, 2, 4, 6,9, 13, 22 Get your method to work with 'nice' data first, then think about any special cases and address them by adjusting your code if needed: either array could be empty, one could be longer or shorter than the other, there can be duplicate values. Any others? 3. Include big-O analysis of your algorithm's space and time complexity in a comment preceding your method. This will not implement a full merge sort, but only the merge step that merges two already sorted arrays Add a method void merge(intll param) to SmartArray. It must merge the sorted parameter array into a sorted SmartArray, maintaining the order in the resulting array. You will provide the parameter array 1. Note that the implementation will require a third array to merge into. We want the behavior as outlined below. Implement the method such that the SmartArray's free space before and after the merge is equal. 'Free space' refers to the difference between data.length and elements, the available storage slots. 2. Test the method in main: called 'smart, say,-7,2, // assume a sorted instance of SmartArray 4, 9, 13 smart.print(); // will print-7, 2, 4,9, 13 smart.merge(new int[l i-9, 1, 6, 221); smart.print); //must print the merged data -9,-7,1, 2, 4, 6,9, 13, 22 Get your method to work with 'nice' data first, then think about any special cases and address them by adjusting your code if needed: either array could be empty, one could be longer or shorter than the other, there can be duplicate values. Any others? 3. Include big-O analysis of your algorithm's space and time complexity in a comment preceding your method

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions