
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
Implement a sort method
public void sort()
Implement any sort algorithm . Do not use any of Java's built-in sorting algorithms.
i have this code so far:
import java.util.AbstractList;
import java.util.Random;
public class MyLinkedList<E extends Comparable<E>> extends AbstractList<E> {
int size = 0;
Node<E> root = null;
class Node<E> {
E elem;
Node<E> next;
public Node(E elem) {
this.elem = elem;
next = null;
}
}
@Override
public void add(int index, E element) {
Node<E> newNode = new Node(element);
Node<E> temp = root;
for (int i = 1; i < index; i++) {
temp = temp.next;
}
if ((temp == null) || (index == 0)) {
newNode.next = root;
root = newNode;
} else {
newNode.next = temp.next;
temp.next = newNode;
}
size++;
}
@Override
public E remove(int index) {
E result = null;
Node<E> temp = root;
for (int i = 1; i < index; i++) {
temp = temp.next;
}
if (root == null) {
result = null;
} else if ((temp == null) || (index == 0)) {
result = root.elem;
root = root.next;
} else {
result = temp.next.elem;
temp.next = temp.next.next;
}
size--;
return result;
}
@Override
public E set(int index, E element) {
E replaced = remove(index);
add(index, element);
return replaced;
}
@Override
public E get(int index) {
Node<E> temp = root;
for (int i = 0; i < index; i++) {
if (temp != null) {
temp = temp.next;
} else {
return null;
}
}
if (temp != null) {
return temp.elem;
}
return null;
}
@Override
public int size() {
return size;
}
public void shuffle(long seed) {
Random random = new Random(seed);
Node<E> temp = root;
while (temp != null) {
int index = random.nextInt(size);
E data = temp.elem;
temp.elem = get(index);
set(index, data);
temp = temp.next;
}
}
public void sort() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - 1 - i; j++) {
E elem1 = get(j);
E elem2 = get(j + 1);
if (elem1.compareTo(elem2) > 0) {
set(j, elem2);
set(j + 1, elem1);
}
}
}
}
}
import java.util.Random;
public class MyLinkedList<E extends Comparable<E>> extends AbstractList<E> {
int size = 0;
Node<E> root = null;
class Node<E> {
E elem;
Node<E> next;
public Node(E elem) {
this.elem = elem;
next = null;
}
}
@Override
public void add(int index, E element) {
Node<E> newNode = new Node(element);
Node<E> temp = root;
for (int i = 1; i < index; i++) {
temp = temp.next;
}
if ((temp == null) || (index == 0)) {
newNode.next = root;
root = newNode;
} else {
newNode.next = temp.next;
temp.next = newNode;
}
size++;
}
@Override
public E remove(int index) {
E result = null;
Node<E> temp = root;
for (int i = 1; i < index; i++) {
temp = temp.next;
}
if (root == null) {
result = null;
} else if ((temp == null) || (index == 0)) {
result = root.elem;
root = root.next;
} else {
result = temp.next.elem;
temp.next = temp.next.next;
}
size--;
return result;
}
@Override
public E set(int index, E element) {
E replaced = remove(index);
add(index, element);
return replaced;
}
@Override
public E get(int index) {
Node<E> temp = root;
for (int i = 0; i < index; i++) {
if (temp != null) {
temp = temp.next;
} else {
return null;
}
}
if (temp != null) {
return temp.elem;
}
return null;
}
@Override
public int size() {
return size;
}
public void shuffle(long seed) {
Random random = new Random(seed);
Node<E> temp = root;
while (temp != null) {
int index = random.nextInt(size);
E data = temp.elem;
temp.elem = get(index);
set(index, data);
temp = temp.next;
}
}
public void sort() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - 1 - i; j++) {
E elem1 = get(j);
E elem2 = get(j + 1);
if (elem1.compareTo(elem2) > 0) {
set(j, elem2);
set(j + 1, elem1);
}
}
}
}
}
however i am having trouble with my output i.e.
it is supposed to be
Small data set\n
[E, A, D, B, C]\n
Sort\n
[A, B, C, D, E]\n
Clearing\n
[ ]\n // two spaces here ...
[E, A, D, B, C]\n
Sort\n
[A, B, C, D, E]\n
Clearing\n
[ ]\n // two spaces here ...
but it is outputting:
Small data set\n
[E, A, D, B, C]\n
Sort\n
[A, B, C, D, E]\n
Clearing\n
[ ]\n // only one space ...
[E, A, D, B, C]\n
Sort\n
[A, B, C, D, E]\n
Clearing\n
[ ]\n // only one space ...
can an expert help fix my code so that it creates two spaces in the brackets of the clearing, that is the only part of my ouput that is incorrect
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 5 steps with 6 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Java 1. Implement ArrayUnorderedList<T> class which will extend ArrayList<T> by defining the following additional methods(i) public void addToFront(T element); //Adds the specified element to the front of the list.(ii) public void addToRear(T element); //Adds the specified element to the rear of the list.(iii) public void addAfter(T element, T target); //Adds the specified element after the specified target element.a. Create an object of ArrayUnorderedList<T> class and perform some add, remove, and search operations and finally print the entire Stack.arrow_forward44 // create a loop that reads in the next int from the scanner to the intList 45 46 47 // call the ReverseArray function 48 49 50 System.out.print("Your output is: "); 51 // print the output from ReverseArray 52 ww 53 54 55 }arrow_forwardpackage edu.umsl.iterator;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Iterator;public class Main {public static void main(String[] args) {String[] cities = {"New York", "Atlanta", "Dallas", "Madison"};Collection<String> stringCollection = new ArrayList<>(Arrays.asList(cities));Iterator<String> iterator = stringCollection.iterator();while (iterator.hasNext()) {System.out.println(/* Fill in here */);}}} Rewrite the while loop to print out the collection using an iterator. Group of answer choices iterator.toString() iterator.getClass(java.lang.String) iterator.remove() iterator.next()arrow_forward
- Please answer the problem in the screenshot. Please use the methods below as a base. The language is in Java. import java.util.*; class HeapMax { // we go with arraylist instead of array for size flexibility private ArrayList<Integer> data; // DO NOT MODIFY THIS METHOD public HeapMax() { data = new ArrayList<Integer>(0); } // insert a new element and restore max heap property public void insert(int element) { } // return max public int getMax() { // remove this line return 0; } // remove max and restore max heap property public int removeMax() { // remove this line return 0; } // heap builder public void build(int[] arr) { } // print out heap as instructed in the handout public void display() { } // you are welcome to add any supporting methods }arrow_forwardimport java.util.HashSet; import java.util.Set; // Define a class named LinearSearchSet public class LinearSearchSet { // Define a method named linearSearch that takes in a Set and an integer target // as parameters public static boolean linearSearch(Set<Integer> set, int target) { // Iterate over all elements in the Set for () { // Check if the current value is equal to the target if () { // If so, return true } } // If the target was not found, return false } // Define the main method public static void main(String[] args) { // Create a HashSet of integers and populate integer values Set<Integer> numbers = new HashSet<>(); // Define the target to search for numbers.add(3); numbers.add(6); numbers.add(2); numbers.add(9); numbers.add(11); // Call the linearSearch method with the set…arrow_forwardWrite a Java program for a matrix class that can add and multiply arbitrary two dimensional arrays of integers. Textbook Project P-3.36, pp. 147 Implement Singly Linked List - use textbook Chapter 3.2 as an exaple. Write a main driver to test basic list implementations. Textbook reference : Data structures and algorithms in Java Micheal Goodricharrow_forward
- public class PokerAnalysis implements PokerAnalyzer { privateList<Card>cards; privateint[]rankCounts; privateint[]suitCounts; /** * The constructor has been partially implemented for you. cards is the * ArrayList where you'll be adding all the cards you're given. In addition, * there are two arrays. You don't necessarily need to use them, but using them * will be extremely helpful. * * The rankCounts array is of the same length as the number of Ranks. At * position i of the array, keep a count of the number of cards whose * rank.ordinal() equals i. Repeat the same with Suits for suitCounts. For * example, if your Cards are (Clubs 4, Clubs 10, Spades 2), your suitCounts * array would be {2, 0, 0, 1}. * * @param cards * the list of cards to be added */ publicPokerAnalysis(List<Card>cards){ this.cards=newArrayList<Card>(); this.rankCounts=newint[Rank.values().length]; this.suitCounts=newint[Suit.values().length];…arrow_forwardImplement a Single linked list to store a set of Integer numbers (no duplicate) • Instance variable• Constructor• Accessor and Update methods 2.) Define SLinkedList Classa. Instance Variables: # Node head # Node tail # int sizeb. Constructorc. Methods # int getSize() //Return the number of nodes of the list. # boolean isEmpty() //Return true if the list is empty, and false otherwise. # int getFirst() //Return the value of the first node of the list. # int getLast()/ /Return the value of the Last node of the list. # Node getHead()/ /Return the head # setHead(Node h)//Set the head # Node getTail()/ /Return the tail # setTail(Node t)//Set the tail # addFirst(E e) //add a new element to the front of the list # addLast(E e) // add new element to the end of the list # E removeFirst()//Return the value of the first node of the list # display()/ /print out values of all the nodes of the list # Node search(E key)//check if a given…arrow_forwardImplementation of largest-cover difference #23@title Implementation of largest-cover difference def rectangle_difference(r, t): "Computes the rectangle difference rt, and outputs the result as a list of rectangles." assert len(r) == len(t), "Rectangles have different dimensions" ### YOUR SOLUTION HERE In the above code, we notice how it paid off to encapsulate intervals in their own abstraction. Now that we need to reason about rectangles, we do not need to get bogged down into complicated considerations of how to perform difference; the comparisons between endpoints are all done in the context of intervals, where it is easier to reason about them. It is often the case that problems become easy, once you think at them in the appropriate context. It is much easier to develop the code for the difference of intervals, as we have done, and then move on to difference of rectangles, rather than trying to write code for rectangle difference directly. The result is also far more elegant. # Let…arrow_forward
- InfoProcessorTest.java import java.util.*; /** * * Class to process and extract information from a list of lines. * */ public class InfoProcessorTest { /** * * List of lines to process. * */ private ArrayList<String> lines =new ArrayList<String>(); /** * * Creates InfoProcessor with given list of lines. * * @param lines to process * */ public InfoProcessorTest(ArrayList<String> lines) { this.lines = lines; } /** * * Gets the course name from the list of lines. * * First, finds the line that starts with "Course:". * * Second, gets the String on the very next line, which should be the course * name. * * Third, returns the String from the method. * * * * Hint(s): * * - Use the getNextStringStartsWith(String str) method to find the course name * * * * Example(s): * * - If the ArrayList<String> lines contains: "Course:" and "CIT590", and we * call * * getCourseName(), we'll get "CIT590". * * * * - If the ArrayList<String> lines contains: "Course:"…arrow_forwardimport java.util.*; public class Main{ public static void main(String[] args) { Main m = new Main(); m.go(); } private void go() { List<Stadium> parks = new ArrayList<Stadium>(); parks.add(new Stadium("PNC Park", "Pittsburgh", 38362, true)); parks.add(new Stadium("Dodgers Stadium", "Los Angeles", 56000, true)); parks.add(new Stadium("Citizens Bank Park", "Philadelphia", 43035, false)); parks.add(new Stadium("Coors Field", "Denver", 50398, true)); parks.add(new Stadium("Yankee Stadium", "New York", 54251, false)); parks.add(new Stadium("AT&T Park", "San Francisco", 41915, true)); parks.add(new Stadium("Citi Field", "New York", 41922, false)); parks.add(new Stadium("Angels Stadium", "Los Angeles", 45050, true)); Collections.sort(parks, Stadium.ByKidZoneCityName.getInstance()); for (Stadium s : parks) System.out.println(s); }}…arrow_forwardSorting Create a MyLinkedList class with inner Node class, data fields, and the insert(element) method. Implement a toString method to return all nodes in MyLinkedList. Implement a recursive sorting and a non-recursive sorting method.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education