Question

In: Computer Science

I need to write a method that sorts a provided Linked list with bubble sort and...

I need to write a method that sorts a provided Linked list with bubble sort and using ONLY Java List Iterator Methods (Link Below)

https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html  

  public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {

    // first line to start you off

    ListIterator<E> iit = c.listIterator(), jit;

    /**** Test code: do not modify *****/

    List cc = new LinkedList(c);

    Collections.sort(c);

    ListIterator it1 = c.listIterator(), it2 = cc.listIterator();

while (it1.hasNext()) {

if (!it1.next().equals(it2.next()))

        throw new Exception("List not sorted");

}

    /***********************************/

  }

Solutions

Expert Solution

Here is the completed code for this method. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//required method implementation

public static <E extends Comparable<? super E>> void bubbleSort(List<E> c)

                                throws Exception {

                // first line to start you off

                ListIterator<E> iit = c.listIterator(), jit;

                int index = 0; // index of current element the iterator iit is iterating

                // looping until list is fully sorted

                while (iit.hasNext()) {

                                System.out.println(c);

                                // getting an iterator

                                jit = c.listIterator();

                                // declaring two E objects to null

                                E ob1 = null, ob2 = null;

                                // setting ob1 to first element

                                if (jit.hasNext()) {

                                               ob1 = jit.next();

                                }

                                // loops as long as there is an element left to read

                                while (jit.hasNext()) {

                                               ob2 = jit.next();

                                               // comparing ob1 and ob2

                                               if (ob1.compareTo(ob2) > 0) {

                                                               // ob1 and ob2 needs to be swapped

                                                               // this can be done by removing last two values iterated and

                                                               // adding in reverse order

                                                               jit.previous();

                                                               jit.previous(); // now the iterator cursor is at ob1

                                                               // removing ob1

                                                               jit.remove();

                                                               // moving to ob2

                                                               jit.next();

                                                               // removing ob2

                                                               jit.remove();

                                                               // adding ob2, and then ob1

                                                               jit.add(ob2);

                                                               jit.add(ob1);

                                                               // resetting iit so that ConcurrentModificationException is

                                                               // not thrown, starting iteration from index 'index'

                                                               iit = c.listIterator(index);

                                               } else {

                                                               ob1 = ob2;

                                               }

                                }

                                //calling next element in iit, updating index

                                iit.next();

                                index++;

                }

                /**** Test code: do not modify *****/

                List cc = new LinkedList(c);

                Collections.sort(c);

                ListIterator it1 = c.listIterator(), it2 = cc.listIterator();

                while (it1.hasNext()) {

                                if (!it1.next().equals(it2.next()))

                                               throw new Exception("List not sorted");

                }

}


Related Solutions

Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Please use the code I provided!! Use either bubble sort or selection sort!! Thank you in...
Please use the code I provided!! Use either bubble sort or selection sort!! Thank you in advance This lab involves adding a sorting function to an existing C++ template. In this module, a file is provided for you -- SortableBag.h -- that implements a simple "bag" (unsorted collection of numbers) structure with array storage. Part 1 Add a sort() method to this template, which should not return any values or take any arguments ('void' for both the return type and...
How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h>...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h> using namespace std; struct ListNode { int data; ListNode *next; ListNode(int x) : data(x), next(nullptr) {} }; class LinkedList { private: ListNode *head = nullptr; public: void addNode(int x) { ListNode *p = new ListNode(x); if (head == nullptr) head = p; else { ListNode *q = head; while (q->next != nullptr) q = q->next; q->next = p; } } void display() { ListNode...
NEED: INSERTION SORT AND BINARY SEARCH IMMEDIATE NEED: Implement 2 searches and 2 sorts, and write...
NEED: INSERTION SORT AND BINARY SEARCH IMMEDIATE NEED: Implement 2 searches and 2 sorts, and write test programs to convince someone that you did them right. DIRECTIONS: You have been provided: 1. Function stubs for searching and sorting algorithms: Utility functions: swap, shift right/left, show array, insertion point. Searching: linear seach, binary search Sorting: bubble sort, insertion sort, selection sort. 2. Main program providing a pattern for testing youour functions, including: - sample arrays, each providing a different test case....
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here       ...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT