Question: Write a java program that implements three sorting algorithms(bubble sort, selection sort, and mergesort) and one binary search algorithms): - allows user input for entering

Write a java program that implements three sorting algorithms(bubble sort, selection sort, and mergesort) and one binary search algorithms):

- allows user input for entering which sorting or search algorithm to use;

- then allow the user to choose either string/names or numbers,

- then use input validation for any exception errors.

- use object-oriented programming with different classes in order to reuse the code.

- rectify any other problems or suitable code injections.

Please use this code below to update any other missing like adding mergesort and binary sort and also update the main test switch type to add case 2 for mergesort and case for binary search.

//*****************************MainDriver************************************************

mport java.util.Arrays; import java.util.Scanner; public class MainDriver { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int size, type; do { System.out.print("Enter the size of the array (>0): "); size = scanner.nextInt(); System.out.println (); } while (size <= 0); do { System.out.print("Enter 1 for strings or 2 for integers: "); type = scanner.nextInt(); } while (type < 1 || type > 2); scanner.nextLine(); System.out.println (); // Switch statement switch (type) { case 1: { String array1[] = new String[ size ]; String array2[] = new String[ size ]; for (int i = 0; i < size; i++) { System.out.print ("Enter a string : "); array2[ i ] = array1[ i ] = scanner.nextLine (); } System.out.println (); System.out.println ("Sorting array 1 using bubble sort ..."); BubbleSort.bubbleSort (array1); System.out.println ("Array 1:"); System.out.println (Arrays.toString (array1)); System.out.println (" "); System.out.println ("Sorting array 2 using selection sort ..."); BubbleSort.bubbleSort (array2); System.out.println ("Array 2:"); System.out.println (Arrays.toString (array2)); break; } default: { int array1[] = new int[ size ]; int array2[] = new int[ size ]; for (int i = 0; i < size; i++) { System.out.print ("Enter an integer : "); array2[ i ] = array1[ i ] = scanner.nextInt (); } System.out.println ("Sorting array 1 using bubble sort ..."); BubbleSort.bubbleSort (array1); System.out.println ("Sorting array 2 using selection sort ..."); BubbleSort.bubbleSort (array2); System.out.println ("Array 1:"); System.out.println (Arrays.toString (array1)); System.out.println ("Array 2:"); System.out.println (Arrays.toString (array2)); break; } } } } 

//********************************BubbleSort class*************************************************************************

public class BubbleSort { // Sorts string using bubble sort public static void bubbleSort(String[] str) { String temp = ""; for (int i = 0; i < str.length; i++) { for (int j = i + 1; j < str.length; j++) { if (str[i].compareTo(str[j]) > 0) { temp = str[i]; str[i] = str[j]; str[j] = temp; } } } } // Sorts integer using bubble sort public static void bubbleSort(int[] nums) { int temp = 0; for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] > nums[j]) { temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } } } }

//****************************SelectionSort**********************************************

public class SelectionSort { //********************************** // Sorts integers using selection sort public static void selectionSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int currentMin = arr[i]; int currentMinIndex = i; for (int j = i + 1; j < arr.length; j++) { if (currentMin > arr[j]) { currentMin = arr[j]; currentMinIndex = j; } } if (currentMinIndex != i) { arr[currentMinIndex] = arr[i]; arr[i] = currentMin; } } } //*********************************************** // Sorts strings using selection sort public static void selectionSort(String[] str) { for (int i = 0; i < str.length - 1; i++) { String currentMin = str[i]; int currentMinIndex = i; for (int j = i + 1; j < str.length; j++) { if (currentMin.compareTo(str[j]) > 0) { currentMin = str[j]; currentMinIndex = j; } } if (currentMinIndex != i) { str[currentMinIndex] = str[i]; str[i] = currentMin; } } } } 

//******************************************************************************************

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!