Question: JAVA eclipse please help! Modify the Sorting lab program to allow for a range of sort. The left index is inclusive and the right index

JAVA eclipse please help!

Modify the Sorting lab program to allow for a range of sort. The left index is inclusive and the right index is exclusive.

1.

public static void bubbleSort(int[] array, int left, int right) {

// your code here

}

2.

public static void insertionSort(int[] array, int left, int right) {

// your code here

}

3.

public static void selectionSort(int[] array, int left, int right) {

// your code here

}

View required output

Test Case 1

Standard Input Files in the same directory
4 3 2 1ENTER quitENTER 

SortTester.java

Please enter a space separated list of numbers to sort or type QUIT to exit: Sorting first half of the array using Bubble Sort algorithm: 3 4 2 1 Sorting second half of the array using Bubble Sort algorithm: 4 3 1 2 Sorting the array using Bubble Sort algorithm: 1 2 3 4 Sorting first half of the array using Insertion Sort algorithm: 3 4 2 1 Sorting second half of the array using Insertion Sort algorithm: 4 3 1 2 Sorting the array using Insertion Sort algorithm: 1 2 3 4 Sorting first half of the array using Selection Sort algorithm: 3 4 2 1 Sorting second half of the array using Selection Sort algorithm: 4 3 1 2 Sorting the array using Selection Sort algorithm: 1 2 3 4 Please enter a space separated list of numbers to sort or type QUIT to exit: 

Test Case 2

Standard Input Files in the same directory
-1 -2 -3 -4ENTER quitENTER 

SortTester.java

Please enter a space separated list of numbers to sort or type QUIT to exit: Sorting first half of the array using Bubble Sort algorithm: -2 -1 -3 -4 Sorting second half of the array using Bubble Sort algorithm: -1 -2 -4 -3 Sorting the array using Bubble Sort algorithm: -4 -3 -2 -1 Sorting first half of the array using Insertion Sort algorithm: -2 -1 -3 -4 Sorting second half of the array using Insertion Sort algorithm: -1 -2 -4 -3 Sorting the array using Insertion Sort algorithm: -4 -3 -2 -1 Sorting first half of the array using Selection Sort algorithm: -2 -1 -3 -4 Sorting second half of the array using Selection Sort algorithm: -1 -2 -4 -3 Sorting the array using Selection Sort algorithm: -4 -3 -2 -1 Please enter a space separated list of numbers to sort or type QUIT to exit: 

Test Case 3

Standard Input Files in the same directory
QuitENTER 

SortTester.java

Please enter a space separated list of numbers to sort or type QUIT to exit: 

Test Case 4

Standard Input Files in the same directory
0ENTER qUitENTER 

SortTester.java

Please enter a space separated list of numbers to sort or type QUIT to exit: Sorting first half of the array using Bubble Sort algorithm: 0 Sorting second half of the array using Bubble Sort algorithm: 0 Sorting the array using Bubble Sort algorithm: 0 Sorting first half of the array using Insertion Sort algorithm: 0 Sorting second half of the array using Insertion Sort algorithm: 0 Sorting the array using Insertion Sort algorithm: 0 Sorting first half of the array using Selection Sort algorithm: 0 Sorting second half of the array using Selection Sort algorithm: 0 Sorting the array using Selection Sort algorithm: 0 Please enter a space separated list of numbers to sort or type QUIT to exit: 

Test Case 5

Standard Input Files in the same directory
1 2 3 10 9 7 8 9 9 9 9 9 1 1 1 1 2 2 2 2 10 10 10 7 7 7 -100ENTER QUIT

SortTester.java

Please enter a space separated list of numbers to sort or type QUIT to exit: Sorting first half of the array using Bubble Sort algorithm: 1 1 2 3 7 8 9 9 9 9 9 9 10 1 1 1 2 2 2 2 10 10 10 7 7 7 -100 Sorting second half of the array using Bubble Sort algorithm: 1 2 3 10 9 7 8 9 9 9 9 9 1 -100 1 1 1 2 2 2 2 7 7 7 10 10 10 Sorting the array using Bubble Sort algorithm: -100 1 1 1 1 1 2 2 2 2 2 3 7 7 7 7 8 9 9 9 9 9 9 10 10 10 10 Sorting first half of the array using Insertion Sort algorithm: 1 1 2 3 7 8 9 9 9 9 9 9 10 1 1 1 2 2 2 2 10 10 10 7 7 7 -100 Sorting second half of the array using Insertion Sort algorithm: 1 2 3 10 9 7 8 9 9 9 9 9 1 -100 1 1 1 2 2 2 2 7 7 7 10 10 10 Sorting the array using Insertion Sort algorithm: -100 1 1 1 1 1 2 2 2 2 2 3 7 7 7 7 8 9 9 9 9 9 9 10 10 10 10 Sorting first half of the array using Selection Sort algorithm: 1 1 2 3 7 8 9 9 9 9 9 9 10 1 1 1 2 2 2 2 10 10 10 7 7 7 -100 Sorting second half of the array using Selection Sort algorithm: 1 2 3 10 9 7 8 9 9 9 9 9 1 -100 1 1 1 2 2 2 2 7 7 7 10 10 10 Sorting the array using Selection Sort algorithm: -100 1 1 1 1 1 2 2 2 2 2 3 7 7 7 7 8 9 9 9 9 9 9 10 10 10 10 Please enter a space separated list of numbers to sort or type QUIT to exit: 

___________________________________________________________________________________

Sortester.java code:

import java.util.Scanner;

public class SortTester {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter a space separated list of numbers to sort or type QUIT to exit:");

String numbers = keyboard.nextLine();

while (!numbers.equalsIgnoreCase("quit")) {

int[] array = makeArray(numbers);

System.out.println("Sorting first half of the array using Bubble Sort algorithm:");

Sort.bubbleSort(array, 0, array.length / 2);

println(array);

array = makeArray(numbers);

System.out.println("Sorting second half of the array using Bubble Sort algorithm:");

Sort.bubbleSort(array, array.length / 2, array.length);

println(array);

array = makeArray(numbers);

System.out.println("Sorting the array using Bubble Sort algorithm:");

Sort.bubbleSort(array, 0, array.length);

println(array);

array = makeArray(numbers);

System.out.println("Sorting first half of the array using Insertion Sort algorithm:");

Sort.insertionSort(array, 0, array.length / 2);

println(array);

array = makeArray(numbers);

System.out.println("Sorting second half of the array using Insertion Sort algorithm:");

Sort.insertionSort(array, array.length / 2, array.length);

println(array);

array = makeArray(numbers);

System.out.println("Sorting the array using Insertion Sort algorithm:");

Sort.insertionSort(array, 0, array.length);

println(array);

array = makeArray(numbers);

System.out.println("Sorting first half of the array using Selection Sort algorithm:");

Sort.selectionSort(array, 0, array.length / 2);

println(array);

array = makeArray(numbers);

System.out.println("Sorting second half of the array using Selection Sort algorithm:");

Sort.selectionSort(array, array.length / 2, array.length);

println(array);

array = makeArray(numbers);

System.out.println("Sorting the array using Selection Sort algorithm:");

Sort.selectionSort(array, 0, array.length);

println(array);

System.out.println("Please enter a space separated list of numbers to sort or type QUIT to exit:");

numbers = keyboard.nextLine();

}

keyboard.close();

}

public static int[] makeArray(String str) {

if (!str.trim().isEmpty()) {

String[] strings = str.trim().split("[ ]+");

int[] result = new int[strings.length];

for (int i = 0; i < result.length; i++)

result[i] = Integer.parseInt(strings[i]);

return result;

}

else {

return new int[0];

}

}

public static boolean equals(int[] array1, int[] array2) {

boolean equal = true;

if (array1.length != array2.length)

equal = false;

int i = 0;

while (equal && i < array1.length) {

equal = (array1[i] == array2[i]);

}

return equal;

}

public static void println(int[] array) {

for (int i = 0; i < array.length; i++) {

if (i == array.length - 1)

System.out.println(array[i]);

else

System.out.print(array[i] + " ");

}

}

}

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!