Answered step by step
Verified Expert Solution
Question
1 Approved Answer
java Make a class called SortSearchTest in SortSearchTest.java Inside of the SortSearchTest class, create unit tests for the sort and search methods: testMergesort(): testQuickSort(): testBinarySearch():
java Make a class called SortSearchTest in SortSearchTest.java
Inside of the SortSearchTest class, create unit tests for the sort and search methods:
testMergesort():
testQuickSort():
testBinarySearch():
here are my methods
//mergesort
public static void mergesort(ArrayListarrA, SortSearchCriteria criteria) { int sizeOfA = arrA.size(); if (sizeOfA > 1) { //debug = true; if (debug) { //'debug' never getting the 'true' value, either initialise it to 'true' //or don't write the if-block System.out.println("A begin: " + arrA); } int halfSizeofA = sizeOfA / 2; ArrayList arrB = new ArrayList<>(arrA.subList(0, halfSizeofA)); ArrayList arrC = new ArrayList<>(arrA.subList(halfSizeofA, sizeOfA)); mergesort(arrB, criteria); mergesort(arrC, criteria); arrA = merge(arrB, arrC, arrA, criteria); //need help implementing it if (debug) { //'debug' never getting the 'true' value and this will never execute System.out.println("A end: " + arrA); } } }
//quick sort
public static ArrayListquickSort(ArrayList stu, SortSearchCriteria criteria) { // Checks if the ArrayList object per size is less than or equals to 1 if (stu.size() <= 1) // Return the list because it is already sorted return stu; // Otherwise create ArrayList for sorted ArrayList sortedList = new ArrayList (); // Otherwise create ArrayList for lesser ArrayList lesserList = new ArrayList (); // Otherwise create ArrayList for greater ArrayList greaterList = new ArrayList (); // Stores the last Student object as pivot Student pivot = stu.get(stu.size() - 1); // Loops till per size minus one times for (int i = 0; i < stu.size() - 1; i++) { // Checks if enumeration SearchCriteria constant is NAME if (criteria == SortSearchCriteria.NAME) { // Checks if ArrayList current object name is less than the pivot name if (stu.get(i).getName().compareTo(pivot.getName()) < 0) // Adds the object to lesserList ArrayList lesserList.add(stu.get(i)); // Otherwise greater else // Adds the object to greaterList ArrayList greaterList.add(stu.get(i)); }// End of if condition // Otherwise enumeration SearchCriteria constant is AGE else { // Checks if ArrayList current object age is less than the pivot age if (stu.get(i).getAge() < pivot.getAge()) // Adds the object to lesserList ArrayList lesserList.add(stu.get(i)); // Otherwise greater else // Adds the object to lesserList ArrayList greaterList.add(stu.get(i)); }// End of else }// End of for loop // Recursively call the method with ArrayList lesserList lesserList = quickSort(lesserList, criteria); // Recursively call the method with ArrayList greaterList greaterList = quickSort(greaterList, criteria); // Adds the pivot object to lesserList lesserList.add(pivot); // Adds the greaterList to lesserList lesserList.addAll(greaterList); // Assigns the lesserList to sortedList sortedList = lesserList; // Returns the sortedList return sortedList; }// End of method
//binary search
public static void binarySearch(ArrayListarrA, String name) { int left = 0, right = arrA.size() - 1; while (left <= right) { int mid = (left + right) / 2; if (arrA.get(mid).getName().equals(name)) { System.out.println("Student's name found:" + name); return; //if name found then get out of the method with this return //other wise it will keep on printing this statement } else if (arrA.get(mid).getName().compareTo(name) > 0) { right = mid - 1; } else { left = mid + 1; } } System.out.println("Student not found"); } public static void binarySearch(ArrayList arrA, int age) { int left = 0, right = arrA.size() - 1; while (left <= right) { int mid = (left + right) / 2; if (arrA.get(mid).getAge() == age) { System.out.println("Student's age found:" + age); return; //if age found then get out of the method } else if (arrA.get(mid).getAge() > age) { right = mid - 1; } else { left = mid + 1; } } System.out.println("Student not found"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started