Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi I need help writing a short method in java in relation to data structures and algorithms. We are aiming for O(n): so a single

Hi I need help writing a short method in java in relation to data structures and algorithms. We are aiming for O(n): so a single for-loop. I have started working on it, but cannot seem to figure it out. Any help would be greatly appreciated, and please include comments so that i may understand. Please find the criteria as well as my code so far down below:

image text in transcribed

public class SmarterArray {

// data

private int[] data;

private int elements;

public static long before;

public static long after;

// constructor method, returns nothing

public SmarterArray()

{

data = new int[4]; // as the size goes up, the sort takes longer

elements = 0;

}

// method which doubles the size of an array, does not return anything

public void DoubleSize()

{

int[] tmp = new int[2 * data.length];

// move the existing values/data

for (int i = 0; i

{

// moving into tmp array

tmp[i]=data[i];

}

// have original array point to new/temp. one

data = tmp;

}

// this methods adds to the end of an array

public void add(int val)

{

if(elements >= data.length)

{

DoubleSize();

}

data[elements] = val;

elements++;

}

// this method prints the array values

public void print()

{

// prints only valid data

for(int i = 0; i

{

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

}

System.out.println();

}

// find min, and return index

public int min()

{

// make an assumption that min is 1st element

int loc = 0;

// compare against the remainder

for(int i = 1; i

{

// change the previous assumption if necessary

if(data[i]

{

loc = i;

}

}

// return the location

return loc;

}

// find max, and return its index

public int max()

{

// make an assumption that min is the first element

int loc = 0;

// compare against the remainder

for(int i = 1; i

{

// change assumption if necessary

if(data[i] > data[loc])

{

loc = i;

}

}

// return location

return loc;

}

// this method gets 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;

}

// this method sorts the data in array in ascending order

public void sort()

{

// creating a temp. array that is the same size as the original array (data)

int[] tmp2 = new int[data.length];

// transferring data to temp. array using a for loop

for(int i = 0;i

{

int loc = this.min();

tmp2[i] = this.get(loc);

data[loc] = this.get(this.max())+1;

}

// make original array point to new/temporary array

data = tmp2;

}

public void insert(int val)

{

// step 1: if the array is full, increase its size

if(elements >= data.length)

{

DoubleSize();

}

for(int i = 0; i

{

}

// step 5: increment the variable 'elements'

elements++;

}

// main method

public static void main(String[] args)

{

SmarterArray smarter = new SmarterArray();

// adding in numbers to sort

smarter.add(15);

smarter.add(6);

smarter.add(7);

smarter.add(9);

smarter.add(-4);

smarter.print();

//sorting our data

smarter.sort();

smarter.print();

// inserting more values into our array, while maintaining the ordered sort

smarter.insert(8); // insert in between

smarter.print(); // prints -4 6 7 8 9 15

smarter.insert(-9); // insert at the beginning

smarter.print(); // prints -9 -4 6 7 8 9 15

smarter.insert(19); // insert at the end

smarter.print(); // prints -9 -4 6 7 8 9 15 19

}

}

The algorithm must complete these functional steps in some way: insert(value) 1. 2. 3. 4. 5. if the array is full, increase its size find the index where value must get inserted create a gap at that index, push existing data one space towards the end copy the new value into the gap increment the variable 'elements' Steps 2 and 3 are unique to insert(). The other steps were in some form already present in add(). Note that this is one way to express the algorithm, not the only way, but the functional pieces have to be represented in some way. Adjust your own algorithm to include all the essential parts. The implementation will also likely look different for different people. Perhaps especially steps 2 and 3 can be implemented in various ways. Step 1 should call the existing method for doubling the array's size. Implement the algorithm and test it thoroughly. Cover all the cases: insert at the beginning, end, or in between. Depending on your implementation these may work right away, but you have to test them to verify, and modify the code if needed. Have your main program demonstrate these cases. Suppose your array contains the values f-4, 6, 7, 9,15]: array.print(); array.insert(8); array.print(); array.insert(-9); array.print() array.insert(19); array.print(); // prints -467915 // insert in between // prints -4 678915 //insert at the beginning // prints -9-4678915 // insert at the end // prints -9-4 6789 15 19

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

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions

Question

3. Did you seek anyones advice?

Answered: 1 week ago

Question

7. What traps should she avoid?

Answered: 1 week ago

Question

5. What decision-making model would you advocate to this person?

Answered: 1 week ago