Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Java

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

}

Files in the same directory

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] + " ");

}

}

}

Standard Input Files in the same directory
4 3 2 1 quit 

SortTester.java

Required Output

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 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

Have issues been prioritized?

Answered: 1 week ago

Question

Has the priority order been provided by someone else?

Answered: 1 week ago

Question

Compare the current team to the ideal team.

Answered: 1 week ago