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> extends AbstractList { int size = 0; Node root = null; class Node { E elem; Node next; public Node(E elem) { this.elem = elem; next = null; } } @Override public void add(int index, E element) { Node newNode = new Node(element); Node 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 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 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 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 ... 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 ... 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
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> extends AbstractList { int size = 0; Node root = null; class Node { E elem; Node next; public Node(E elem) { this.elem = elem; next = null; } } @Override public void add(int index, E element) { Node newNode = new Node(element); Node 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 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 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 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 ... 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 ... 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
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
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 now
This is a popular solution!
Step by step
Solved 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.Recommended textbooks for you

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

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