Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Bubble Sort is a simple algorithm used for sorting a list L of n comparable elem

ID: 3683173 • Letter: B

Question

Bubble Sort is a simple algorithm used for sorting a list L of n comparable elements. This algorithm scans the list n-1 times, where in each scan, the algorithm compares the current element with the next one and swaps them if they are out of order. Implement Bubble Sort efficiently using Doubly Linked List. What is the running time of the algorithm? Code in Java

Explanation / Answer

public void bubbleSort() { boolean done = false; while (!done) { Node cur = head; done = true; while(cur != tail) { if (cur.getNext().getCount()>cur.getCount()) { swap(cur.getNext(),cur); done=false; } cur = cur.getNext(); } } } private static class Node { int count; Node next; } private void swap(Node node1, Node node2) { int aux = node1.getCount(); node1.setCount(node2.getCount()); node2.setCount(aux); }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote