Add a method insertionSort to the class ArraySorter, as given in Listing 7.10, mat performs an insertion sort of an array. To simplify this protect, our insertion sort
Inserting into a sorted array
Insertion sort algorithm to sort an array
for (index = 0; index <a.length; index++)
Insert the value of a [index] into its correct position in the array temp, so that all the elements copied into the array temp so far are sorted.
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
Software Engineering (10th Edition)
Starting Out with C++: Early Objects (9th Edition)
Using MIS (10th Edition)
Database Concepts (7th Edition)
Database Concepts (8th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
- Min Heap of Candy — Add and Remove Using the provided code (Question04.java and Candyjava), write the add and remove methods for a min heap of Candy using an array implementation. Make sure to include all provided files in your project and write your solution in the file "Question04.java". This method should preserve the properties of a min hap, and the candies are compared based on their weight. This value can be accessed through its accessor. Solution Tests: • Does the solution compile? • Does the solution have your name in the comments? • Does the solution have a high-level solution description (150-300 words) in the comments? • Does the method preserve the properties of a Min Heap? • Does the output match the following? Testing the Add Method Candy Weight: 10.0, Candy Weight: 40.0, Candy Weight: 20.0, Candy Weight: 50.0, Candy Weight: 60.0, Candy Weight: 80.0, Candy Weight: 30.0, Candy Weight: 100.0, Candy Weight: 70.0, Candy Weight: 90.0, Testing the Remove Method Candy Weight:…arrow_forwardWrite 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_forward3. Remove an element from an array Consider an array named source. Write a method/function named remove( source, size, idx) that removes the element in index idx of the source array. You must execute the method by passing an array, its size and the idx( that is the index of the element to be removed). After calling the method, print the array to show whether the element of that particular index has been removed properly. Example: source=[10,20,30,40,50,0,0] remove(source,5,2) After calling remove(source,5,2) , printing the array should give the output as: [ 10,20,40,50,0,0,0] Use Python languagearrow_forward
- 3. Remove an element from an array Consider an array named source. Write a method/function named remove( source, size, idx) that removes the element in index idx of the source array. You must execute the method by passing an array, its size and the idx( that is the index of the element to be removed). After calling the method, print the array to show whether the element of that particular index has been removed properly. Example: source=[10,20,30,40,50,0,0] remove(source,5,2) After calling remove(source,5,2) , printing the array should give the output as: [ 10,20,40,50,0,0,0] [Use Python and Don't use Built-in function like "pop"]arrow_forwardINSERTION 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_forwardWrite in Java. Given an array of integers A, find the Majority Element. Majority Element in an array of size N in an element that appears more than N/2 times. Write a function: int findMajority(int[] A) that accepts an array A. The function should return the Majority Element in the array. If no majority element then return 0. Use following methods to solve the problem: int findCandidate(int a[]) that accepts the array and find a candidate for the majority boolean isMajority(int a[], int cand) that accept the array and the candidate element and check if the candidate occurs more than n/2 times Input: 5 1 2 1 2 2 Where, First line represents the size of an array. Second line represents array elements separated by single space. Output: 2 Here for the given array, 2 appears 3 times in the array of size 5. No space after the element in the output. Assume that, N is an integer within the range [1 to 10000]. Array elements are within the range…arrow_forward
- Write in Java. Given an array of integers A, find the Majority Element. Majority Element in an array of size N in an element that appears more than N/2 times. Write a function: int findMajority(int[] A) that accepts an array A. The function should return the Majority Element in the array. If no majority element then return 0. Use following methods to solve the problem: int findCandidate(int a[]) that accepts the array and find a candidate for the majority boolean isMajority(int a[], int cand) that accept the array and the candidate element and check if the candidate occurs more than n/2 times Input: 5 1 2 1 2 2 Where, First line represents the size of an array. Second line represents array elements separated by single space. Output: 2 Here for the given array, 2 appears 3 times in the array of size 5. No space after the element in the output. Assume that, N is an integer within the range [1 to 10000]. Array elements are within the range…arrow_forwardWrite in java code Create an array myArr of 10 integer elements and initialize/fill it with numbers (not sorted) between 0 and 20; for example myArr = [ 12, 3, 19, 5, 7, 11,….etc.]. (a) print the array. (b) Use method sort() of class Arrays to sort myArr and print it after sorting. (c) Use the arraycopy() method of class System to copy myArr to another array called my2ndArr, then print both arrays. (d) use method equals() of class Arrays between the two arrays and print the results. (e) use method fill() to fill my2ndArr with 99 in all elements and then use method equals()and print the result whether the two arrays are equal or not. (f) use the method binarySearch() to search myArr for the value 15 and print the resultarrow_forwardWrite a Java program tat prompts user for a list of integers with 0 as the last value.Save the values in an array. Assume there can be maximum 100 values. Theprogram should have the following methods:- A method that takes the array as parameter and updates each value in thearray to the square of the value.- Another method that takes the original and modified arrays as parametersand displays the original and the squared values.arrow_forward
- URGENT 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_forwardFirst, write a Java method merge that takes as parameter a non-empty double array representing employees’ salaries. The method merges the values of every 2 adjacent elements. put the merged values at the beginning of the array, and put 0 in the remaining positions. Suppose that the original array will contain an even number of elements. Second, write two java methods readArray(int[]a) and printArray(int[]a) that reads/writes the elements of an array passed in parameter from/to the standard input/output. Finally, Use the following main method to test your different methods: public class MergeSalary { private static Scanner input = new Scanner(System.in); public static void main(String[] args) { } } double[] salaries = new double[10]; System.out.print("Enter 10 salaries: "); readArray(salaries); merge(salaries); System.out.print("Salaries after merge: "); printArray(salaries); Hint: For the method merge, you can work on the same array or use a temporary array. Run example:…arrow_forwardFirst, write a Java method merge that takes as parameter a non-empty double array representing employees’ salaries. The method merges the values of every 2 adjacent elements. put the merged values at the beginning of the array, and put 0 in the remaining positions. Suppose that the original array will contain an even number of elements. Second, write two java methods readArray(int[]a) and printArray(int[]a) that reads/writes the elements of an array passed in parameter from/to the standard input/output. Finally, Use the following main method to test your different methods:arrow_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