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 : Introduction To Prob...-MyProgrammingLab
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)
- First, 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_forwardDefine a class Car as follows:class Car { public String make; public String model; public int mpg;// Miles per gallon}a) Implement a comparator called CompareCarsByMakeThenModel that can be passed as an argument to the quicksort method from the lecture notes. CompareCarsByMakeThenModel should return a value that will cause quicksort to sort an array of cars in ascending order (from smallest to largest) by make and, when two cars have the same make, in ascending order by model.b) Implement a comparator called CompareCarsByDescendingMPG that can be passed as an argument to the quicksort method from the lecture notes. CompareCarsByDescendingMPG should return a value that will cause quicksort to sort an array of cars in descending order (from largest to smallest) by mpg.c) Implement a comparator called CompareCarsByMakeThenDescendingMPG that can be passed as an argument to the quicksort method from the lecture notes.…arrow_forward
- write a java program: (must use set or map) Given an integer array and a value k, write a method called kDifference() thatfinds a pair in the array such that their difference equals k. If such a pair existsonly print the first one you find, otherwise state that there is no pair.arrow_forwardComplete the implementation of scrabble.c, such that it determines the winner of a short scrabble-like game, where two players each enter their word, and the higher scoring player wins. Notice that we’ve stored the point values of each letter of the alphabet in an integer array named POINTS. For example, A or a is worth 1 point (represented by POINTS[0]), B or b is worth 3 points (represented by POINTS[1]), etc. Notice that we’ve created a prototype for a helper function called compute_score() that takes a string as input and returns an int. Whenever we would like to assign point values to a particular word, we can call this function. Note that this prototype is required for C to know that compute_score() exists later in the program. In main(), the program prompts the two players for their words using the get_string() function. These values are stored inside variables named word1 and word2. In compute_score(), your program should compute, using the POINTS array, and return the score…arrow_forwardrun the main() method of class Lab11D. Pls do the SWAPIN LAB 11D and below is the tester For example, A B C D E F should turn into D E F A B C. You should assume that the array list has an even number of elements (not necessarily 6). One solution is to keep removing the element at index 0 and adding it to the back. Each step would look like this: A B C D E F B C D E F A C D E F A B D E F A B C When you run the code, you will find that there is a lot of movement in the array list. Each call to remove(0) causes n - 1 elements to move, where n is the length of the array. If n is 100, then you move 99 elements 50 times, (almost 5000 move operations). That's an inefficient way of swapping the first and second halves. (Learn more about time complexity at the end of the lab). Come up with a better way in which you swap the elements directly. Hint: How do you swap the two elements A and D? A B C D E F D B C A E F D E C A B F D E F A B C Write pseudocode for this algorithm. Add…arrow_forward
- Write a Java method that takes two 2 dimensional int arrays (a and b) anda 2 dimensional boolean array (c) where all three arrays have the samesize. Your method should return an array (result) such that, if a particular element in c is true, then the corresponding (same indexed) elementin result must be the multiplication of the the corresponding elements ofa and b. If the element in c is false, then the the corresponding elementin result must be the negative of multiplication of the the correspondingelement in a and b.arrow_forwardSuppose that the bag class is efficiently implemented with a fixed array with a capacity of 4000, as in Chapter 3 of the class text. We execute these statements: bag b; b.insert(5); b.insert(4); b.insert(6); b.erase_one(5); Group of answer choices b.used is 2, b.data[0] is 4, b.data[1] is 6 b.used is 2, b.data[0] is 6, b.data[1] is 4 b.used is 3, b.data[0] is 4, b.data[1] is 6 b.used is 3, b.data[0] is 6, b.data[1] is 4arrow_forwardThe implementation of a queue in an array, as given in this chapter, uses the variable count to determine whether the queue is empty or full. You can also use the variable count to return the number of elements in the queue. On the other hand, class linkedQueueType does not use such a variable to keep track of the number of elements in the queue. Redefine the class linkedQueueType by adding the variable count to keep track of the number of elements in the queue. Modify the definitions of the functions addQueue and deleteQueue as necessary. Add the function queueCount to return the number of elements in the queue. Also, write a program to test various operations of the class you defined.arrow_forward
- Write a program to overload the function call operator ( ) so as to allow the more common form of double-array subscripting. Therefore, instead of saying: chessBoard[row][column] for an array of objects, overload the function call operator to allow the alternate form: chessBoard(row, column) A sample output of your program should look like follows: The value of each array element is the product of the row and column values. Using the class definition given in the myClassOperator.h header file below, implement the class member functions and driver code in separate files.arrow_forwardUSING JAVA First, write a Java method isSequence that takes as parameters a non-empty 2-dimensional array of integers. The method checks whether the elements of the array are consecutive or not. Second, write a java methods printArray(int[][]a) that writes the elements of an array passed in parameter to the standard output. Finally, use the following main method to test your different methods: public class CheckSequence { public static void main(String[] args) { int[][] array1 = {{5,6,7,8},{9,10,11,12},{13,14,15,16}, {17,18,19,20}}; int[][] array2 = {{1, 3 , 2,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; if (isSequence(array1)) { System.out.println("The following array elements are successive:"); printArray(array1); } if (isSequence(array2)) { System.out.println("The following array elements are successive:"); printArray(array2); } } } Run Example: The following array elements are successive: 56789 10 11 1213 14 15 16 17 18 19 20arrow_forwardUsing Java - write a new class called Rack, to store an ArrayList of Tiles, to sort them using the Selection Sort algorithm, and finish the program. Code: class Main { publicstaticvoid main(String[] args) { Rack craigTiles = new Rack(); craigTiles.addTile(new Tile('H', 4)); craigTiles.addTile(new Tile('E', 1)); craigTiles.addTile(new Tile('L', 1)); craigTiles.addTile(new Tile('L', 1)); craigTiles.addTile(new Tile('O', 1)); craigTiles.addTile(new Tile('Z', 10)); craigTiles.addTile(new Tile('Y', 4)); craigTiles.sortHighToLow(); System.out.println("Craig's rack of tiles, in order from most valuable to least:"); System.out.println(craigTiles); Rack bettoTiles = new Rack(); bettoTiles.addTile(new Tile('E', 1)); bettoTiles.addTile(new Tile('A', 1)); bettoTiles.addTile(new Tile('S', 1)); bettoTiles.addTile(new Tile('D', 2)); bettoTiles.addTile(new Tile('W', 4)); bettoTiles.addTile(new Tile('T', 1)); bettoTiles.addTile(new Tile('N', 1)); bettoTiles.sortHighToLow();…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning