Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me code the following in: JAVA! Please read the instructions THOROUGHLY and use many COMMENTS! Full points will be awarded, thanks in advance!

Please help me code the following in: JAVA!

Please read the instructions THOROUGHLY and use many COMMENTS!

Full points will be awarded, thanks in advance!

image text in transcribedimage text in transcribed

MyArrayList class:

import java.util.*;

import java.util.Comparator;

/**

* This class implements multiple sort algorithms to be used with ints, chars, and Stings.

* Bubble sort, Selection sort, and Insertion sorts are implemented here

*/

public class MyArrayList {

// instance data

protected int[] IntList;

protected char[] CharList;

protected String[] StringList;

// constructor will build all 3 arrays here

public MyArrayList() {

this.IntList = new int[10];

this.CharList = new char[10];

this.StringList = new String[5];

// fill all 3 arrays with data

for(int i = 0; i

IntList[i] = (int) (Math.random()*52);

}

// Populate char array

for(int i = 0; i

Random random = new Random();

CharList[i] = (char)(random.nextInt(26) + 'a');

}

// Populate String array

StringList[0] = "joe";

StringList[1] = "mark";

StringList[2] = "abbey";

StringList[3] = "tony";

StringList[4] = "kevin";

}

public int compareTo(MyArrayList other) {

// your code here

System.out.println("compareTo() is returning -1, array[0]

}

// your code here

System.out.println("compareTo() is returning 1, array[0] > other[0]");

}

else {

System.out.println("compareTo() is returning 0, array[0] != other[0] ");

// return a value here

}

}

public void intBubbleSort() {

// Implement your sort, call a helper swap method

}

public void CharBubbleSort() {

// Implement your sort, call a helper swap method

}

public void stringBubbleSort() {

// Implement your sort, call a helper swap method

}

public void swapInts(int[] intList, int j) {

// code for swapping ints

}

public void swapChars(char[] charList, int j) {

// code for swapping chars

}

public void swapStrings(String[] stringList, int j) {

// code for swapping Strings

}

//selection sort for ints

public void selectionSort() {

// Implement your sort, call swapSelection(int[] intList, int i, int nextMin)

}

//selection sort for Strings

public void stringSelectionSort() {

// Implement your sort, call swapSelectionStrings(String[] StringList, int i)

// and findSmallest(IntList, i, IntList.length) from your method

}

public void swapSelection(int[] intList, int i, int nextMin) {

// Your code here to swap int values

}

public void swapSelectionStrings(String[] StringList, int i) {

// Your code here to swap values

}

public int findSmallest(int[] arr, int begin, int end) {

//returns the index of the smallest element

int minIndex = begin; //hint

for(int i = begin; i

if(arr[begin - 1]

minIndex = begin;

} else

minIndex = begin - 1;

}

return minIndex;

}

//Insertion Sort

public void insertionSort() {

for(int i = 0; i

/ote -1 above since were dealing with neighbors (a, a+1)

int current = IntList[i];

//int hole = i;

while( i > 0 && IntList[i - 1] > current ) { //while out of place

//slide data to the left moving the hole left

.

. // more code goes here

.

.

}

}

}

}

InsertionSort In this section, we will explore a new sorting implementation that grows a sorted partition by one at each step. As we expand our sorted partition to include our "nearest neighbor", our partition becomes unsorted until we put the newest neighbor in the correct spot. So, our strategy will be outlined and developed below by our pseudocode below; if you think you've got a handle on the mechanism, try to iteration (spoiler alert!) if stumped InsertionSort Pseudocode, Iteration 1 Starting with the first node, ending with the last Get its neighbor (i+1) While the neighbor is out of place, move it backwards through the sorted partition Once the neighbor is in the right place, we're (locally) sorted and its time to repeat this process InsertionSort Implementation (1) In the same ArrayList class, declare a method called insertionSort(); (2) Implement the above (or below) pseudocode logic in your code. (3) In your main test driver, change the code so that it invokes the InsertionSort. a. Test your code

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

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions