diff --git a/a2/src/edu/wmich/cs3310/a2/Controller.java b/a2/src/edu/wmich/cs3310/a2/Controller.java index 1f0a59de6fa7656a5e7a1182624412f29f8ca422..4ab073f7f22ee30632d5a86a25d85fc1e2634da9 100644 --- a/a2/src/edu/wmich/cs3310/a2/Controller.java +++ b/a2/src/edu/wmich/cs3310/a2/Controller.java @@ -1,7 +1,10 @@ package edu.wmich.cs3310.a2; -import java.util.Random; +import java.util.Arrays; import java.util.Scanner; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; import edu.wmich.cs3310.a2.DataStructures.LinkedNode; import edu.wmich.cs3310.a2.DataStructures.LinkedQueue; @@ -24,6 +27,7 @@ public class Controller { private char[] mergeArray; private char[] insertArray; private char[] bInsertArray; + private Map<String, int[]> sortingDictionary; private LinkedQueue bubbleList = new LinkedQueue(); private LinkedQueue selectList = new LinkedQueue(); private LinkedQueue mergeList = new LinkedQueue(); @@ -58,6 +62,8 @@ public class Controller { nameString = GetUserInput("Enter a name: "); PopulateData(); + PopulateDictionary(); + PrintAllDictionaryValues(); PrintAllDataStructures(); SortAllDataStructures(); PrintAllDataStructures(); @@ -110,6 +116,98 @@ public class Controller { } } + + /** + * Populates sorting dictionary based on user's provided name. + */ + protected void PopulateDictionary() { + int index; + int totalIndex = 0; + int[] intArray; + int[] tempArray; + char[] charArray = nameString.toCharArray(); + sortingDictionary = new HashMap<String, int[]>(); + + // Loop through all chars in provided name. + while (totalIndex < charArray.length) { + + // Check if given key value exists within dictionary. + tempArray = sortingDictionary.get(Character.toString(charArray[totalIndex])); + + if (tempArray == null) { + // No value found. Creating new entry in dictionary. + System.out.println("NULL RETURNED FOR " + charArray[totalIndex]); + + intArray = new int[1]; + intArray[0] = totalIndex; + sortingDictionary.put(Character.toString(charArray[totalIndex]), intArray); + } else { + // Previous value found. Appending index to previous entry. + System.out.println("DUPLICATE CHAR FOUND " + charArray[totalIndex]); + + // Create new array of +1 size and copy all old values. + intArray = new int[tempArray.length + 1]; + int index2 = 0; + while (index2 < tempArray.length) { + intArray[index2] = tempArray[index2]; + index2++; + } + + // Add new value and insert back into dict. + intArray[index2] = totalIndex; + sortingDictionary.put(Character.toString(charArray[totalIndex]), intArray); + } + totalIndex++; + } + + // Now that name is handled, add all remaining alphabet characters to dict. + charArray = "abcdefghijklmnopqrstuvwxyz".toCharArray(); + for (index = 0; index < charArray.length; index++) { + + // Check if given key value exists within dictionary. + tempArray = sortingDictionary.get(Character.toString(charArray[index])); + if (tempArray == null) { + // No value found. Creating new entry in dictionary. + System.out.println("NULL RETURNED FOR " + charArray[index]); + + intArray = new int[1]; + intArray[0] = totalIndex; + sortingDictionary.put(Character.toString(charArray[index]), intArray); + totalIndex++; + } + } + } + + + /** + * Checks to see if charA is less than charB. + * Instead of standard ascii values, it uses custom dict values that are based off input name. + * + * @param charA Expected smaller character. + * @param charB Expected larger character. + * @return 1 for true. 0 for equal. -1 if charB is larger than charA. + */ + protected int CompareChars(char charA, char charB) { + int[] arrayA; + int[] arrayB; + + // Check if equal. + if (charA == charB) { + return 0; + } else { + arrayA = sortingDictionary.get(Character.toString(charA)); + arrayB = sortingDictionary.get(Character.toString(charB)); + + // TODO: Currently only checks first value in array. + // IE: If name has duplicate letters, only first letter has any effect. + if (arrayA[0] < arrayB[0]) { + return 1; + } else { + return -1; + } + } + } + //region Sorting Methods /** @@ -146,7 +244,7 @@ public class Controller { // Loop through all unsorted values. while (index < (maxIndex - 1)) { // Check current and next values. - if (bubbleArray[index] > bubbleArray[index + 1]) { + if (CompareChars(bubbleArray[index + 1], bubbleArray[index]) == 1) { tempChar = bubbleArray[index]; bubbleArray[index] = bubbleArray[index + 1]; bubbleArray[index + 1] = tempChar; @@ -176,7 +274,7 @@ public class Controller { // Loop through all unsorted values. while (currentNode != lastUnsortedNode) { // Check current and next values. - if (currentNode.data() > currentNode.next().data()) { + if (CompareChars(currentNode.next().data(), currentNode.data()) == 1) { tempChar = currentNode.data(); currentNode.data(currentNode.next().data()); currentNode.next().data(tempChar); @@ -210,7 +308,7 @@ public class Controller { for (index = 0; index < lastUnsortedIndex; index++) { // Compare values. - if (selectArray[currentIndex] > selectArray[selectedIndex]) { + if (CompareChars(selectArray[selectedIndex], selectArray[currentIndex]) == 1) { selectedIndex = currentIndex; } currentIndex++; @@ -247,7 +345,7 @@ public class Controller { while(currentNode != null) { // Compare values. - if (currentNode.data() < selectedNode.data()) { + if (CompareChars(currentNode.data(), selectedNode.data()) == 1) { selectedIndex = currentIndex; selectedNode = currentNode; } @@ -306,13 +404,15 @@ public class Controller { mergeArray[index] = tempArray[rightHolder]; rightHolder++; } else { + // Check if right side is done but left is not. if (rightHolder > highIndex) { mergeArray[index] = tempArray[leftHolder]; leftHolder++; } else { + // Both sides not done. Compare left and right holder values. - if (tempArray[leftHolder] > tempArray[rightHolder]) { + if (CompareChars(tempArray[rightHolder], tempArray[leftHolder]) == 1) { mergeArray[index] = tempArray[rightHolder]; rightHolder++; } else { @@ -364,7 +464,7 @@ public class Controller { } firstHalf = ListMergeSort(firstHalf, lowIndex, midIndex); secondHalf = ListMergeSort(secondHalf, (midIndex + 1), highIndex); - sortedQueue = MergeList(firstHalf, secondHalf, lowIndex, midIndex, highIndex); + sortedQueue = MergeList(firstHalf, secondHalf); return sortedQueue; } @@ -374,11 +474,8 @@ public class Controller { * Handles actually merging list parts back together. * @param firstHalf First queue section to merge. * @param secondHalf Second queue section to merge. - * @param lowIndex Starting index of first half. - * @param midIndex Ending index of first half/starting index of second half. - * @param highIndex Ending index of second half. */ - protected LinkedQueue MergeList(LinkedQueue firstHalf, LinkedQueue secondHalf, int lowIndex, int midIndex, int highIndex) { + protected LinkedQueue MergeList(LinkedQueue firstHalf, LinkedQueue secondHalf) { LinkedNode tempNode; LinkedQueue sortedQueue = new LinkedQueue(); @@ -390,13 +487,15 @@ public class Controller { tempNode = secondHalf.Dequeue(); sortedQueue.Enqueue(tempNode.data()); } else { + // Check if right side is done but left is not. if (secondHalf.headNode() == null) { tempNode = firstHalf.Dequeue(); sortedQueue.Enqueue(tempNode.data()); } else { + // Both sides not done. compare left and right values. - if (firstHalf.headNode().data() > secondHalf.headNode().data()) { + if (CompareChars(secondHalf.headNode().data(), firstHalf.headNode().data()) == 1) { tempNode = secondHalf.Dequeue(); sortedQueue.Enqueue(tempNode.data()); } else { @@ -425,7 +524,7 @@ public class Controller { // Compare values. Continually loop until 0 index is reached or proper order is found. tempIndex = highestIndex; - while ((currentIndex > 0) && (insertArray[currentIndex] < insertArray[tempIndex])) { + while ((currentIndex > 0) && (CompareChars(insertArray[currentIndex], insertArray[tempIndex]) == 1)) { tempChar = insertArray[currentIndex]; insertArray[currentIndex] = insertArray[tempIndex]; insertArray[tempIndex] = tempChar; @@ -452,7 +551,7 @@ public class Controller { for (currentIndex = 1; currentIndex < charNumber; currentIndex++) { // Compare values. Continually loop until null pointer or proper order is found. tempNode = highestNode; - while ((currentNode.prev() != null) && (currentNode.data() < tempNode.data())) { + while ((currentNode.prev() != null) && (CompareChars(currentNode.data(), tempNode.data()) == 1)) { tempChar = currentNode.data(); currentNode.data(tempNode.data()); tempNode.data(tempChar); @@ -476,7 +575,7 @@ public class Controller { for (currentIndex = 1; currentIndex < charNumber; currentIndex++) { // Compare values. If difference is found, call binary insert. - if (bInsertArray[currentIndex] < bInsertArray[highestIndex]) { + if (CompareChars(bInsertArray[currentIndex], bInsertArray[highestIndex]) == 1) { ArrayBinaryInsert(0, currentIndex); } highestIndex++; @@ -503,7 +602,7 @@ public class Controller { tempIndex = highIndex; // Insert value at appropriate location. - if (bInsertArray[lowIndex] > tempChar) { + if (CompareChars(tempChar, bInsertArray[lowIndex]) == 1) { bInsertArray[tempIndex] = bInsertArray[lowIndex]; tempIndex--; } @@ -515,7 +614,7 @@ public class Controller { midIndex = (lowIndex + highIndex) / 2; // Check for value match. - if (bInsertArray[midIndex] == bInsertArray[highIndex]) { + if (CompareChars(bInsertArray[midIndex], bInsertArray[highIndex]) == 0) { tempChar = bInsertArray[highIndex]; tempIndex = highIndex; while (tempIndex > midIndex) { @@ -525,7 +624,7 @@ public class Controller { bInsertArray[midIndex] = tempChar; } else { - if (bInsertArray[midIndex] > bInsertArray[highIndex]) { + if (CompareChars(bInsertArray[highIndex], bInsertArray[midIndex]) == 1) { // Move all prior values down by one since second half of list was eliminated. tempIndex = highIndex; @@ -555,8 +654,8 @@ public class Controller { // Loop until all values are sorted. for (currentIndex = 1; currentIndex < charNumber; currentIndex++) { - // Compare values. Continually loop until null pointer or proper order is found. - if (currentNode.data() < highestNode.data()) { + // Compare values. If difference is found, call binary insert. + if (CompareChars(currentNode.data(), highestNode.data()) == 1) { ListBinaryInsert(bInsertList.headNode(), currentNode, 0, currentIndex); } highestNode = highestNode.next(); @@ -586,7 +685,7 @@ public class Controller { tempChar = highNode.data(); // Insert value at appropriate location. - if (lowNode.data() > tempChar) { + if (CompareChars(tempChar, lowNode.data()) == 1) { lowNode.next().data(lowNode.data()); highNode = highNode.prev(); } @@ -602,7 +701,7 @@ public class Controller { } // Check for value match. - if (midNode.data() == highNode.data()) { + if (CompareChars(midNode.data(), highNode.data()) == 0) { tempChar = highNode.data(); while (highNode != midNode.next()) { @@ -613,7 +712,7 @@ public class Controller { ListBinaryInsert(lowNode, midNode, lowIndex, midIndex); } else { - if (midNode.data() > highNode.data()) { + if (CompareChars(highNode.data(), midNode.data()) == 1) { // Move all prior values down by one since second half of list was eliminated. tempChar = highNode.data(); @@ -668,6 +767,21 @@ public class Controller { } + /** + * Prints all key-value pairs within dictionary. + */ + protected void PrintAllDictionaryValues() { + + System.out.println("Printing dictionary values..."); + + for (Map.Entry<String, int[]> entry : sortingDictionary.entrySet()) { + String key = entry.getKey(); + int[] value = entry.getValue(); + + System.out.println(" " + key + ": " + Arrays.toString(value)); + } + } + /** * Prints out values of all available data structures. */