Skip to content
Snippets Groups Projects
Commit d58e10c3 authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Update searches to account for duplicate characters

parent 7a6dd64e
Branches
No related merge requests found
......@@ -16,7 +16,9 @@ public class Controller {
private int arrayDim1;
private int arrayDim2;
private char[][] unsortedArray;
private char [][] unsortedFoundHolder;
private char[] sortedArray;
private char[] sortedFoundHolder;
private Random random = new Random();
private Scanner reader = new Scanner(System.in);
private ComputeTime computeTime = new ComputeTime();
......@@ -132,14 +134,14 @@ public class Controller {
private void SortArray() {
int arrayIndex1;
int arrayIndex2;
int tempArrayIndex = 0;
int index = 0;
sortedArray = new char[arrayDim1 * arrayDim2];
// Translate values into sortable 1d-Array.
for (arrayIndex1 = 0; arrayIndex1 < arrayDim1; arrayIndex1++) {
for (arrayIndex2 = 0; arrayIndex2 < arrayDim2; arrayIndex2++) {
sortedArray[tempArrayIndex] = unsortedArray[arrayIndex1][arrayIndex2];
tempArrayIndex++;
sortedArray[index] = unsortedArray[arrayIndex1][arrayIndex2];
index++;
}
}
......@@ -155,8 +157,19 @@ public class Controller {
private void SearchNameUnsorted(String name) {
int stringLength = name.length();
int index = 0;
int arrayIndex1;
int arrayIndex2;
char tempChar;
unsortedFoundHolder = new char[arrayDim1][arrayDim2];
// Create array to hold found values.
for (arrayIndex1 = 0; arrayIndex1 < arrayDim1; arrayIndex1++) {
for (arrayIndex2 = 0; arrayIndex2 < arrayDim2; arrayIndex2++) {
unsortedFoundHolder[arrayIndex1][arrayIndex2] = 'o';
}
}
// Search current string values.
while (index < stringLength) {
tempChar = name.charAt(index);
LinearSearch(tempChar);
......@@ -173,9 +186,22 @@ public class Controller {
private void SearchNameSorted(String name) {
int stringLength = name.length();
int index = 0;
int arrayIndex1;
int arrayIndex2;
int returnIndex;
char tempChar;
sortedFoundHolder = new char[(arrayDim1 * arrayDim2)];
// Create array to hold found values.
for (arrayIndex1 = 0; arrayIndex1 < arrayDim1; arrayIndex1++) {
for (arrayIndex2 = 0; arrayIndex2 < arrayDim2; arrayIndex2++) {
sortedFoundHolder[index] = 'o';
index++;
}
}
// Search current string values.
index = 0;
while (index < stringLength) {
tempChar = name.charAt(index);
returnIndex = BinarySearch(tempChar, 0, ((arrayDim1 * arrayDim2) - 1));
......@@ -202,12 +228,18 @@ public class Controller {
while (arrayIndex2 < arrayDim2) {
if (unsortedArray[arrayIndex1][arrayIndex2] == searchValue) {
// Save found values.
index1 = arrayIndex1;
index2 = arrayIndex2;
// Set vars to exit loops.
arrayIndex1 = arrayDim1;
arrayIndex2 = arrayDim2;
// Match found. Check that value is unique.
if (unsortedFoundHolder[arrayIndex1][arrayIndex2] == 'o') {
// Hold value of index.
unsortedFoundHolder[arrayIndex1][arrayIndex2] = 'x';
// Save found values.
index1 = arrayIndex1;
index2 = arrayIndex2;
// Set vars to exit loops.
arrayIndex1 = arrayDim1;
arrayIndex2 = arrayDim2;
}
}
arrayIndex2++;
}
......@@ -220,16 +252,26 @@ public class Controller {
/**
* Binary search through array for given value.
* @param searchValue Char to search for
* @param searchValue Char to search for.
*/
private int BinarySearch(char searchValue, int firstIndex, int lastIndex) {
int tempInt;
// Check if first index is after last index.
if (firstIndex >= lastIndex) {
// End of search.
if (sortedArray[firstIndex] == searchValue) {
// Match found.
return firstIndex;
// Match found. Check that value is unique.
if (sortedFoundHolder[firstIndex] == 'o') {
// Valid value. Return index.
sortedFoundHolder[firstIndex] = 'x';
return firstIndex;
} else {
// Duplicate value. Return -1.
return -1;
}
} else {
// No match found.
return -1;
......@@ -241,8 +283,28 @@ public class Controller {
int middleIndex = (firstIndex + lastIndex) / 2;
if (sortedArray[middleIndex] == searchValue) {
// Match found.
return middleIndex;
// Match found. Check that value is unique.
if (sortedFoundHolder[middleIndex] == 'o') {
sortedFoundHolder[middleIndex] = 'x';
return middleIndex;
} else {
// First try checking values directly left.
tempInt = BinaryCheckOneLeft(searchValue, middleIndex);
if (tempInt != (-1)) {
return tempInt;
} else {
// No match found. Attempting values directly right.
tempInt = BinaryCheckOneRight(searchValue, middleIndex);
if (tempInt != (-1)) {
return tempInt;
} else {
// No possible matches.
return -1;
}
}
}
} else {
......@@ -257,7 +319,71 @@ public class Controller {
return BinarySearch(searchValue, (middleIndex + 1), lastIndex);
}
}
}
}
/**
* Desired char was found but value was already "used".
* Check values directly left for "unused" match.
* @param searchValue Char to search for.
* @param index Index that was "used".
* @return Valid match or -1.
*/
private int BinaryCheckOneLeft(char searchValue, int index) {
try {
// First check if value one left is same character.
if (sortedArray[index - 1] == searchValue) {
// Match found. Check if spot is unique.
if (sortedFoundHolder[index - 1] == 'o') {
sortedFoundHolder[index - 1] = 'x';
return (index - 1);
} else {
// Next value was already found. Check left again.
return BinaryCheckOneLeft(searchValue, (index - 1));
}
} else {
return -1;
}
} catch(Exception e) {
// Handling if index is out of bounds.
// Obviously no match in this instance.
return -1;
}
}
/**
* Desired char was found but value is already "used".
* Check values directly right for "unused" match.
* @param searchValue Char to serach for.
* @param index Index that was "used".
* @return Valid match or -1;
*/
private int BinaryCheckOneRight(char searchValue, int index) {
try {
// First check if value one right is same character.
if (sortedArray[index + 1] == searchValue) {
// Match found. Check if spot is unique.
if (sortedFoundHolder[index + 1] == 'o') {
sortedFoundHolder[index + 1] = 'x';
return (index + 1);
} else {
// Next value was already found. Check right again.
return BinaryCheckOneRight(searchValue, (index + 1));
}
} else {
return -1;
}
} catch(Exception e) {
// Handling if index is out of bounds.
// Obviously no match in this instance.
return -1;
}
}
//endregion Array Management Methods
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment