diff --git a/a2/src/edu/wmich/cs3310/a2/Controller.java b/a2/src/edu/wmich/cs3310/a2/Controller.java index 4ab073f7f22ee30632d5a86a25d85fc1e2634da9..5c5ecce667a006b0d49ca9d5264501407332d3ac 100644 --- a/a2/src/edu/wmich/cs3310/a2/Controller.java +++ b/a2/src/edu/wmich/cs3310/a2/Controller.java @@ -20,6 +20,7 @@ public class Controller { private int charNumber; private String nameString; + private boolean showStabilityNumbers; private Random random = new Random(); private Scanner reader = new Scanner(System.in); private char[] bubbleArray; @@ -28,6 +29,7 @@ public class Controller { private char[] insertArray; private char[] bInsertArray; private Map<String, int[]> sortingDictionary; + private Map<String, int[]> stabilityDictionary; private LinkedQueue bubbleList = new LinkedQueue(); private LinkedQueue selectList = new LinkedQueue(); private LinkedQueue mergeList = new LinkedQueue(); @@ -61,8 +63,15 @@ public class Controller { // Get name to modify search with. nameString = GetUserInput("Enter a name: "); - PopulateData(); + // Determine if should show stability of sorts. + tempString = GetUserInput("Show stability of sorts? "); + if ((tempString.equals("y")) || (tempString.equals("ye")) || (tempString.equals("yes"))) { + showStabilityNumbers = true; + } else { + showStabilityNumbers = false; + } PopulateDictionary(); + PopulateData(); PrintAllDictionaryValues(); PrintAllDataStructures(); SortAllDataStructures(); @@ -81,6 +90,7 @@ public class Controller { protected void PopulateData() { int index = 0; int randomInt; + int[] tempArray; LinkedNode tempNode; LinkedQueue tempQueue = new LinkedQueue(); @@ -102,6 +112,7 @@ public class Controller { // Take unsorted list and populate all other data structures with. index = 0; while ((tempNode = tempQueue.Dequeue()) != null) { + bubbleArray[index] = tempNode.data(); bubbleList.Enqueue(tempNode.data()); selectArray[index] = tempNode.data(); @@ -113,6 +124,19 @@ public class Controller { bInsertArray[index] = tempNode.data(); bInsertList.Enqueue(tempNode.data()); index++; + + // Populate optional values for showing stability of sorting methods. + if (showStabilityNumbers) { + tempArray = stabilityDictionary.get(Character.toString(tempNode.data())); + bubbleList.lastNode().stabilityInt(tempArray[0]); + selectList.lastNode().stabilityInt(tempArray[0]); + mergeList.lastNode().stabilityInt(tempArray[0]); + insertList.lastNode().stabilityInt(tempArray[0]); + bInsertList.lastNode().stabilityInt(tempArray[0]); + // Increment char value in dictionary for each time used. + tempArray[0] = tempArray[0] + 1; + stabilityDictionary.put(Character.toString(tempNode.data()), tempArray); + } } } @@ -125,10 +149,22 @@ public class Controller { int totalIndex = 0; int[] intArray; int[] tempArray; - char[] charArray = nameString.toCharArray(); + char[] charArray; sortingDictionary = new HashMap<String, int[]>(); + stabilityDictionary = new HashMap<String, int[]>(); + + // Populate optional dictionary for showing stability of sorting methods. + if (showStabilityNumbers) { + charArray = "abcdefghijklmnopqrstuvwxyz".toCharArray(); + for (index = 0; index < 26; index++) { + intArray = new int[1]; + intArray[0] = 0; + stabilityDictionary.put(Character.toString(charArray[index]), intArray); + } + } // Loop through all chars in provided name. + charArray = nameString.toCharArray(); while (totalIndex < charArray.length) { // Check if given key value exists within dictionary. @@ -262,6 +298,7 @@ public class Controller { * Calls bubble sort on appropriate linked list. */ protected void ListBubbleSort() { + int tempInt; boolean wasSorted = true; char tempChar; LinkedNode currentNode = bubbleList.headNode(); @@ -278,6 +315,11 @@ public class Controller { tempChar = currentNode.data(); currentNode.data(currentNode.next().data()); currentNode.next().data(tempChar); + if (showStabilityNumbers) { + tempInt = currentNode.stabilityInt(); + currentNode.stabilityInt(currentNode.next().stabilityInt()); + currentNode.next().stabilityInt(tempInt); + } wasSorted = true; } currentNode = currentNode.next(); @@ -356,8 +398,10 @@ public class Controller { // Remove found node and add to new "sorted" list. tempNode = selectList.Delete(selectedIndex); tempQueue.Enqueue(tempNode.data()); + if (showStabilityNumbers) { + tempQueue.lastNode().stabilityInt(tempNode.stabilityInt()); + } } - selectList = tempQueue; } @@ -451,8 +495,10 @@ public class Controller { // Left half. tempNode = mergeList.Retrieve(lowIndex); for (index = 0; index < ((midIndex + 1) - lowIndex); index++) { - firstHalf.Enqueue(tempNode.data()); + if (showStabilityNumbers) { + firstHalf.lastNode().stabilityInt(tempNode.stabilityInt()); + } tempNode = tempNode.next(); } @@ -460,6 +506,9 @@ public class Controller { tempNode = mergeList.Retrieve(midIndex + 1); for (index = 0; index < (highIndex - midIndex); index++) { secondHalf.Enqueue(tempNode.data()); + if (showStabilityNumbers) { + secondHalf.lastNode().stabilityInt(tempNode.stabilityInt()); + } tempNode = tempNode.next(); } firstHalf = ListMergeSort(firstHalf, lowIndex, midIndex); @@ -486,21 +535,33 @@ public class Controller { if (firstHalf.headNode() == null) { tempNode = secondHalf.Dequeue(); sortedQueue.Enqueue(tempNode.data()); + if (showStabilityNumbers) { + sortedQueue.lastNode().stabilityInt(tempNode.stabilityInt()); + } } else { // Check if right side is done but left is not. if (secondHalf.headNode() == null) { tempNode = firstHalf.Dequeue(); sortedQueue.Enqueue(tempNode.data()); + if (showStabilityNumbers) { + sortedQueue.lastNode().stabilityInt(tempNode.stabilityInt()); + } } else { // Both sides not done. compare left and right values. if (CompareChars(secondHalf.headNode().data(), firstHalf.headNode().data()) == 1) { tempNode = secondHalf.Dequeue(); sortedQueue.Enqueue(tempNode.data()); + if (showStabilityNumbers) { + sortedQueue.lastNode().stabilityInt(tempNode.stabilityInt()); + } } else { tempNode = firstHalf.Dequeue(); sortedQueue.Enqueue(tempNode.data()); + if (showStabilityNumbers) { + sortedQueue.lastNode().stabilityInt(tempNode.stabilityInt()); + } } } } @@ -542,6 +603,7 @@ public class Controller { */ protected void ListInsertionSort() { int currentIndex; + int tempInt; char tempChar; LinkedNode currentNode = insertList.headNode().next(); LinkedNode tempNode; @@ -552,11 +614,22 @@ public class Controller { // Compare values. Continually loop until null pointer or proper order is found. tempNode = highestNode; while ((currentNode.prev() != null) && (CompareChars(currentNode.data(), tempNode.data()) == 1)) { - tempChar = currentNode.data(); - currentNode.data(tempNode.data()); - tempNode.data(tempChar); - tempNode = tempNode.prev(); - currentNode = currentNode.prev(); + if (showStabilityNumbers) { + tempChar = currentNode.data(); + tempInt = currentNode.stabilityInt(); + currentNode.data(tempNode.data()); + currentNode.stabilityInt(tempNode.stabilityInt()); + tempNode.data(tempChar); + tempNode.stabilityInt(tempInt); + tempNode = tempNode.prev(); + currentNode = currentNode.prev(); + } else { + tempChar = currentNode.data(); + currentNode.data(tempNode.data()); + tempNode.data(tempChar); + tempNode = tempNode.prev(); + currentNode = currentNode.prev(); + } } highestNode = highestNode.next(); currentNode = highestNode.next(); @@ -675,6 +748,7 @@ public class Controller { protected void ListBinaryInsert(LinkedNode lowNode, LinkedNode highNode, int lowIndex, int highIndex) { int index; int midIndex; + int tempInt; char tempChar; LinkedNode midNode; @@ -687,6 +761,9 @@ public class Controller { // Insert value at appropriate location. if (CompareChars(tempChar, lowNode.data()) == 1) { lowNode.next().data(lowNode.data()); + if (showStabilityNumbers) { + lowNode.next().stabilityInt(lowNode.stabilityInt()); + } highNode = highNode.prev(); } highNode.data(tempChar); @@ -704,11 +781,18 @@ public class Controller { if (CompareChars(midNode.data(), highNode.data()) == 0) { tempChar = highNode.data(); + tempInt = highNode.stabilityInt(); while (highNode != midNode.next()) { highNode.data(highNode.prev().data()); + if (showStabilityNumbers) { + highNode.stabilityInt(highNode.prev().stabilityInt()); + } highNode = highNode.prev(); } midNode.next().data(tempChar); + if (showStabilityNumbers) { + midNode.next().stabilityInt(tempInt); + } ListBinaryInsert(lowNode, midNode, lowIndex, midIndex); } else { @@ -716,11 +800,18 @@ public class Controller { // Move all prior values down by one since second half of list was eliminated. tempChar = highNode.data(); + tempInt = highNode.stabilityInt(); while (highNode != midNode) { highNode.data(highNode.prev().data()); + if (showStabilityNumbers) { + highNode.stabilityInt(highNode.prev().stabilityInt()); + } highNode = highNode.prev(); } midNode.data(tempChar); + if (showStabilityNumbers) { + midNode.stabilityInt(tempInt); + } ListBinaryInsert(lowNode, midNode, lowIndex, midIndex); } else { @@ -744,7 +835,7 @@ public class Controller { String tempString; System.out.print("$ "); - tempString = reader.next().trim(); + tempString = reader.next().trim().toLowerCase(); System.out.println(); return tempString; @@ -760,7 +851,7 @@ public class Controller { String tempString; System.out.print(promptString); - tempString = reader.next().trim(); + tempString = reader.next().trim().toLowerCase(); System.out.println(); return tempString; @@ -806,6 +897,15 @@ public class Controller { System.out.print(" " + tempNode.data()); tempNode = tempNode.next(); } + if (showStabilityNumbers) { + System.out.println(); + System.out.print(" "); + tempNode = bubbleList.headNode(); + while (tempNode != null) { + System.out.print(" " + tempNode.stabilityInt()); + tempNode = tempNode.next(); + } + } System.out.println(); System.out.println(); @@ -824,6 +924,15 @@ public class Controller { System.out.print(" " + tempNode.data()); tempNode = tempNode.next(); } + if (showStabilityNumbers) { + System.out.println(); + System.out.print(" "); + tempNode = selectList.headNode(); + while (tempNode != null) { + System.out.print(" " + tempNode.stabilityInt()); + tempNode = tempNode.next(); + } + } System.out.println(); System.out.println(); @@ -842,6 +951,15 @@ public class Controller { System.out.print(" " + tempNode.data()); tempNode = tempNode.next(); } + if (showStabilityNumbers) { + System.out.println(); + System.out.print(" "); + tempNode = mergeList.headNode(); + while (tempNode != null) { + System.out.print(" " + tempNode.stabilityInt()); + tempNode = tempNode.next(); + } + } System.out.println(); System.out.println(); @@ -860,6 +978,15 @@ public class Controller { System.out.print(" " + tempNode.data()); tempNode = tempNode.next(); } + if (showStabilityNumbers) { + System.out.println(); + System.out.print(" "); + tempNode = insertList.headNode(); + while (tempNode != null) { + System.out.print(" " + tempNode.stabilityInt()); + tempNode = tempNode.next(); + } + } System.out.println(); System.out.println(); @@ -878,6 +1005,15 @@ public class Controller { System.out.print(" " + tempNode.data()); tempNode = tempNode.next(); } + if (showStabilityNumbers) { + System.out.println(); + System.out.print(" "); + tempNode = bInsertList.headNode(); + while (tempNode != null) { + System.out.print(" " + tempNode.stabilityInt()); + tempNode = tempNode.next(); + } + } System.out.println(); System.out.println();