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); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.