Professional Documents
Culture Documents
package searchsort;
;
/**
.java - useful methods for searching through arrays and
* Searcher. d lists
* of generic data for a specific item
* @author Mr. Wachs
* @param <T> the generic object used in this class
* @since Feb 9, 2017
* @instructor Mr. Wachs
*/
public class Searcher <T extends Comparable<T>>
{
/**
* an implementation of a linear search (sequential search)
* algorithm
m. It will find the first occurance of an item
* in the LinkedList and return the index where it found it, or a
* -1 if not found
* @param item the generic data type to search for in the list
* @param array an array of generic items to search through
* @return the first index found at, or a -1 if not found
*/
public int linearSearch(TT item, T[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i].equals(item)) {
return i;
}
}
return NOT_FOUND;
}
/**
* an implementation of a linear search (sequential search)
* algorithm
m. It will find the first occurance of an item
* in the LinkedList and return the index where it found it, or a
* -1 if not found
* @param item the generic data type to search for in the list
* @param list the LinkedList to search through
* @return the first index found at, or a -1 if not found
*/
public int linearSearch(T
T item, LinkedList<T> list) {
return recursiveLinearSearch(item, list, 0);
}
/**
* an implementation of a binary search (sequential search)
* algorithm
m. It will find the first occurance of an item
* in the LinkedList and return the index where it found it, or a
* -1 if not found
* @param item the generic data type to search for in the list
* @param array an array of generic items to search through
* @return the first index found at, or a -1 if not found
*/
public int binarySearch(TT item, T[] array) {
int high = array.length - 1;
int low = 0;
while (low <= high) {
int mid = (high + low) / 2;
if (array[mid].compareTo(item) == 0) return mid;
else if (array[mid].compareTo(item) > 0) high = mid - 1;
else if (array[mid].compareTo(item) < 0) low = mid + 1;
}
return NOT_FOUND;
}
/**
* an implementation of a binary search (sequential search)
* algorithm
m. It will find the first occurance of an item
* in the LinkedList and return the index where it found it, or a
* -1 if not found
* @param item the generic data type to search for in the list
* @param list the LinkedList to search through
* @return the first index found at, or a -1 if not found
*/
public int binarySearch(TT item, LinkedList<T> list) {
return recursiveBinarySearch(item,list,0,list.size());
}
/**
* recursive methodd to find
d the first occurance of an item
* in the LinkedList andd return the index where it found
d it, or a
* -1 if not found
* @param item the generic data type to search for in the list
* @param list the LinkedList to search through
* @param i the current index location
* @return the first index found at, or a -1 if not found
*/
private int recursiveLinearSearch(T item, LinkedList<<T> list,
, int i) {
if (i == list.size()) return NOT_FOUND;
else if (item.equals(list.get(i))) return i;
else return recursiveLinearSearch(item,list,i+1);
}
/**
* recursive methodd usedd to find
d the item
m in the list and
d
* return the index of it's location or a -1 if not found
* @param item the generic data type to search for in the list
* @param list the LinkedList to search through
* @param low the lowest index to start from
* @param high the highest index to go to
* @return the first index found at, or a -1 if not found
*/
private int recursiveBinarySearch(T item, LinkedList< <T> list,
,
int low, int high) {
if (low <= high) {
int mid = low + (high - low) / 2;
if (list.get(mid) == null) return NOT_FOUND;
if (list.get(mid).compareTo(item) > 0)
return recursiveBinarySearch(item,list,low,mid-1);
else if (list.get(mid).compareTo(item) < 0)
return recursiveBinarySearch(item,list,mid+1,high);
else
return mid;
}
return NOT_FOUND;
}
}
/** required package class namespace */
package searchsort;
;
/**
.java - useful methods for sorting arrays and
* Sorter. d lists
* @author Mr. Wachs
* @param <T> the generic object used in this class
* @since Feb 9, 2017
* @instructor Mr. Wachs
*/
public class Sorter <T extends Comparable<T>>
{
/**
* a simple implementation of a bubble sort algorithm
* it will sort the array into ascending order
* @param array the array of generic items to sort
*/
public void bubbleSort(TT[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
T item1 = array[j];
T item2 = array[j+1];
if (item1.compareTo(item2) > 0) {
array[j] = item2;
array[j+1] = item1;
}
}
}
}
/**
* an efficient implementation of a bubble sort algorithm
* it will sort the list into ascending order
* @param list the LinkedList to sort
*/
public void bubbleSort(L
LinkedList<T> list) {
boolean sorted = true;
for (int i = list.size() - 1; i >= 0; i--) {
sorted = true;
for (int j = 0; j < i; j++) {
T item1 = list.get(j);
T item2 = list.get(j+1);
if (item1.compareTo(item2) > 0) {
sorted = false;
list.set(j, item2);
list.set(j+1, item1);
}
}
if (sorted) return;
}
}
/**
* an implementation of a selection sort algorithm
* it will sort the array into ascending order
* @param array the array of generic items to sort
*/
public void selectionSort(T T[] array) {
for (int i = 0; i < array.length-1; i++) {
int lowest = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j].compareTo(array[lowest]) < 0) {
lowest = j;
}
}
if (lowest != i) {
T temp = array[i];
array[i] = array[lowest];
array[lowest] = temp;
}
}
}
/**
* an implementation of a selection sort algorithm
* it will sort the list into ascending order
* @param list the LinkedList to sort
*/
public void selectionSort(LLinkedList<T
T> list)
) {
for (int i = 0; i < list.size()-1; i++) {
int lowest = i;
for (int j = i + 1; j < list.size(); j++) {
T item1 = list.get(j);
T item2 = list.get(lowest);
if (item1.compareTo(item2) < 0) {
lowest = j;
}
}
if (lowest != i) {
T temp1 = list.get(i);
T temp2 = list.get(lowest);
list.set(i, temp2);
list.set(lowest, temp1);
}
}
}
}
/** required package class namespace */
package testing.
.searchsort;
/**
.java - the testing done on the Searcher class
* SearchTest.
* @author Mr. Wachs
* @since Mar 1, 2017
* @instructor Mr. Wachs
*/
public class SearchTest
{
}
/** required package class namespace */
package testing.
.searchsort;
/**
.java - the testing done on the Sorter class
* SortTest.
* @author Mr. Wachs
* @since Mar 1, 2017
* @instructor Mr. Wachs
*/
public class SortTest
{
}
/** required package class namespace */
package testing.
.searchsort;
/**
.java - the testing done on both searching and
* SearchSortTest. d sorting, and
* the support methods to do this
* @author Mr. Wachs
* @param <T> the generic object used in this class
* @since Mar 1, 2017
* @instructor Mr. Wachs
*/
public class SearchSortTest <T>
{
/** generates random data to search for in the lists and arrays */
public static void randomSearchItems() {
item1 = Random..generate();
item2 = Random..generate(0,SIZE-1);
item3 = Random..generate(0
0d,,(double)(
(SIZE-1));
item4 = Random..generate('a','z');
item5 = Random..generate(SIZE);
item6 = Random..generate();
item7 = Random..generate(0,SIZE-1);
item8 = Random..generate(0
0d,,(double)(
(SIZE-1));
item9 = Random..generate('a','z');
item10 = Random..generate(SIZE);
}
/** generates search data that is already in the lists and arrays */
public static void locationSearchItems() {
item11 = SearchSortTest.list1.get(R
Random.generate(0, SIZE-1));
item12 = SearchSortTest.list2.get(R
Random.generate(0, SIZE-1));
item13 = SearchSortTest.list3.get(R
Random.generate(0, SIZE-1));
item14 = SearchSortTest.list4.get(R
Random.generate(0, SIZE-1));
item15 = SearchSortTest.list5.get(R
Random.generate(0, SIZE-1
1));
item16 = SearchSortTest.array1[Random.generate(0, SIZE-1)];
item17 = SearchSortTest.array2[Random.generate(0, SIZE-1)];
item18 = SearchSortTest.array3[Random.generate(0, SIZE-1)];
item19 = SearchSortTest.array4[Random.generate(0, SIZE-1)];
item20 = SearchSortTest.array5[Random.generate(0, SIZE-1)];
}
/**
* outputs the search results
* @param <T> the generic object used in this class
* @param text which search results this is
* @param item the item being searched for
* @param location the result of the search
*/
public static <T> void results(S
String text,
, T item,
, int location)
) {
System.
.out.p
println(
(text + "\t result = " + location +
"\t for item " + item.toString());
}
/** sorts all lists and arrays for the binary search */
public static void sortAllListsAndArrays() {
Sorter sorter = new Sorter();
sorter.
.bubbleSort(SearchSortTest.array1);
sorter.
.bubbleSort(SearchSortTest.array2);
sorter.
.bubbleSort(SearchSortTest.array3);
sorter.
.bubbleSort(SearchSortTest.array4);
sorter.
.bubbleSort(SearchSortTest.array5);
sorter.
.bubbleSort(SearchSortTest.list1);
sorter.
.bubbleSort(SearchSortTest.list2);
sorter.
.bubbleSort(SearchSortTest.list3);
sorter.
.bubbleSort(SearchSortTest.list4);
sorter.
.bubbleSort(SearchSortTest.list5);
}
/**
* outputs the contents of an array on the screen
* @param <T> the generic object used in this class
* @param array the array to output
* @return the nicely formatted output of the array
*/
private static <T> String output(TT[] array)) {
String text = "[";
for (int i = 0; i < array.length-1; i++) {
text += array[i].toString() + ",";
}
return text + array[array.length-1].toString() + "]";
}