Add a method bubbleSort to the class ArraySorter, as given in Listing 7.10, that performs a bubble sort of an array. The bubble sort
Bubble sort algorithm to sort an array a
Repeat the following until the array a is sorted:
for (index = 0; index <a. length - 1; index++)
if (a[index] > a [index + 1])
Interchange the values of a [index] and a [index + 1].
The bubble sort algorithm usually requires more time than other sorting methods.
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Additional Engineering Textbook Solutions
Java How To Program (Early Objects)
Starting Out With Visual Basic (8th Edition)
Problem Solving with C++ (9th Edition)
Absolute Java (6th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Software Engineering (10th Edition)
- java code that completes tasks forr an array integers. Swaped the first and last elements in array. Replace all even elements with 0. Shift all elements by one to the right and move the last element into the first position. For example, 1 4 9 16 25 would be turned i into 25 1 4 9 16. Replace each element except first and last the larger of its two neighbors.arrow_forwardWrite a Java class to perform the following: 1. Write a method to search the following array using a linear search, ( target elements: 11, 55, 17.). (count the number of comparisons needed). {06, 02, 04, 07, 11, 09, 50, 62, 43, 32, 13, 75, 01, 46, 88, 17} 2. Write a method to sort the array using Selection Sort. (count the number of comparisons needed) 3, Write a method to sort the array using Bubble Sort. (count the number of comparisons needed) 4, Search he sorted array using a binary search (recursive) for the same set of target elements. (count the number of comparisons needed)arrow_forwardJava Write Java a program that copies an array including the following days: Sat,Sun,Mon,Tue,Wed,Thu,FriThe program shifts the elements in the copied array to the left. Then the program prints the copied array after shifting the elements.arrow_forward
- Write a program that takes numbers from the user and places them into an array. After each new entry from the user, the program prints out the current array of numbers. Several rules must be followed: Each new number is checked and added to the array ONLY if it is larger than the number in the previous position in the array. This condition is not checked for the first number.In other words, the new number is rejected and nothing is added to the array if the entry is too-small. The new number is added to the array EXCEPT: when the user enters a number greater than 75, the number 0 is added to the array instead. when the user enters a multiple of 7 (and less than or equal to 75), the number 7 is added to the array instead. The program exits after 10 numbers have been placed into the array. Programming requirements: You must define and call at least one function. I recommend printArray(...) You must use at least one array. Assume the entries are integers Example Test Case:…arrow_forwardExercise 1: Write a program that generates a sequence of 20 random values between 0 and 99 in an array, prints the sequence, sorts it, and prints the sorted sequence. Use the sort method from the standard Java library. Exercise 2: Write a program that stores a list of countries: "Egypt", "Switzerland", "Argentina", "Spain", "Portugal", "Luxemburg", etc. Initialize your array with a single statement. Then print out the array. Use the sort function as before to sort the countries in alphabetical order. Reprint your array. Exercise 3: Implement exercises 1 and 2 using ArrayList. Use the Collections.sort method for sorting. After you have printed and sorted your results, then append an additional element to each list and reprint your ArrayLists. Notice that the new items will appear just at the end and will not be sorted. Resort and print out the new ArrayLists again. To clarify, you should print out your ArrayLists after your first sort, after adding items with the…arrow_forwardJAVAWrite a program that searches person's name and display all matched name. For example, if a user inputs the letter "Davis", the program searches all name which includes "Davis" in an array. Then, display all matched names on the screen. You must declare a single dimensional array which has all people’s name. Use methods of string classarrow_forward
- Please do not change any of the method signatures in either class. Implement the methods described below. Partition.java public static int partitionLomuto(int[] arr, int low, int high) Implement this method using Lomuto’s partition algorithm. This is the algorithm that we discussed in class that chooses the last element as the partition and iterates through the array swapping an element that belongs in the low side with the lowest (farthest left) element on the high side. At the end, the pivot is swapped with the lowest element (farthest left) element on the high side. This method should return the index of the pivot element, which is now in its correct position in the array. See the pseudocode below. partition(arr, p, r) x = arr[r] i = p – 1 for j = p to r – 1 if arr[j] ≤ x i = i + 1 swap arr[i] and arr[j] swap arr[i+1] and arr[r] return i+1 What sort of input arrays will enable Hoare’s algorithm to still create relatively equal size partitions whereas Lumoto’s algorithm will create…arrow_forwardExtend the Array class adding the following two newmethods:A) the method called min that returns the smallest numberin the array.E.g., applying min to the array [−3, 2, 0, −10, 9, 7], we get −10.B) the method called reverse that reverses the array.E.g., applying reverse to the array [−3, 2, 0, −10, 9, 7], we change itto [7, 9, −10, 0, 2, −3]. USE THE JAVA CODE BELOW TO ADD ON PLEASE public class Array {private int[] elems;private int nElems;public Array(int size) {if (size < 0) {throw new IllegalArgumentException();}elems = new int[size];}public void print() {System.out.print("[");for (int i = 0; i < nElems; ++i)if (i < nElems - 1) {System.out.print(elems[i] + ", ");} else {System.out.print(elems[i]);}System.out.println("]");}public void insert(int element) {// if the array is full, increases its sizeif (elems.length == 0) {int[] extendedElems = new int[1];elems = extendedElems;}if (elems.length == nElems) {int[] extendedElems = new int[nElems * 2];for (int i = 0; i <…arrow_forwardURGENT JAVA Write a Java method that takes two 2 dimensional int arrays (a and b) and a 2 dimensional boolean array (c) where all three arrays have the same size. Your method should return an array (result) such that, if a partic- ular element in c is true, then the corresponding (same indexed) element in result must be the multiplication of the the corresponding elements of a and b. If the element in c is false, then the the corresponding element in result must be the negative of multiplication of the the corresponding element in a and b. For example ifa = {{1,2,3},{4,5}},b = {{1,2,1},{0,2}} andc = {{true,false,true},{false,true}}, then the result should be result={{1, -4, 3}{0, 10}}.arrow_forward
- INSERTION of an element in an array. Write a Java program with the following specifications Size and Numbers are coming from the user input. Given these numbers, the user will insert a value in the array, if the value is lower than the sum of all theelements, then repeat the process. If the above process is able to satisfy the program, the user will enter a position where the value needsto be inserted, if the position is invalid, repeats the process. Display the new set of array.arrow_forwardpublic class ArrayCommonOperations{ public static void main(String [] args) { //*** Task #1: Define and instantiate an array of integer numbers, with 10 elements //*** Task #2: Fill in the array with integer numbers from 1 to 100 //*** Task #3: define another array, named temp, and copy the initial array in it. //* This allows to preserve the original array //*** Task #4: Define the variables you need to calculate the following values, //* and initialize them with appropriate values. //*** Task #5: Print the original array //*** Task #6: Calculate the sum of all values //*** Task #7: Count the number of even values //*** Task #8: Calculate the minimum value in the array //*** Task #9: Calculate the maximum value in the array //*** Task #10: Replace the elements that are divisible by 3, with their value plus 2 //*** Task #11:…arrow_forward4. A segment of an array is defined as a collection of consecutive elements in that array. The length of a segment is the number of elements in the segment. A repeating segment is a segment with all the elements in it having the same value. The following method returns the length of the longest repeating segment in a given int array. Complete the implementation of the method. public int getLength0fLongestRepeatingSegment(int [] array) { 1 // add your implementation here 2 } 3arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education