{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

InsertionSort - InsertionSort.java Author Lewis and Loftus...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
//******************************************************************** // InsertionSort.java Author: Lewis and Loftus // // Solution to Programming Project 12.4 // // Demonstrates the insertion sort using a linked list //******************************************************************** public class InsertionSort { private SortNode list; //----------------------------------------------------------------- // Creates an initially empty linked list. //----------------------------------------------------------------- public InsertionSort() { list = null; } //----------------------------------------------------------------- // Adds an integer to the linked list //----------------------------------------------------------------- public void add(int value) { SortNode node = new SortNode(value); SortNode current = null; if (list == null) list = node; else { current = list; while (current.next != null) current = current.next; current.next = node; } } //---------------------------------------------------------------- // Inserts SortNode in list in ascending order relative // to the other nodes in list. //---------------------------------------------------------------- private void insert (SortNode node) { SortNode current; SortNode previous = null; boolean done = false; if (list == null) list = node; else { current = list; while (!done) { if (current.value > node.value) // add before current { if (previous != null) previous.next = node;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
else list = node; node.next = current; done = true; } else if (current.next == null) // add after current { current.next = node; done = true; } else // move to next
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}