Question: 1.Write a method void showMode(int[] a) that displays the mode(s) of the array a . The mode is the number in the array that appears
1.Write a method void showMode(int[] a) that displays the mode(s) of the array a. The mode is the number in the array that appears most frequently. There may be more than one mode; your code should display all of them. The order in which the modes are displayed doesnt matter. Here are a few examples: {3, 2, 1, 4, 3} Mode(s): 3 {3, 2, 1, 4, 4, 3} Mode(s): 3, 4 {1, 2, 3, 4} Mode(s): 1, 2, 3, 4 Note the void return type this method does not need to return anything. Just have it display the mode(s) when called. Hint: You might start by counting how often each element in the array appears.
2.This problem deals with the problem of partitioning an array. Well define partitioning to mean arranging the elements of an array around a certain pivot element such that all elements to the left of the pivot are strictly less than the pivot, and all elements to the right of the pivot are greater than or equal to the pivot. To simplify things a little bit, well assume that the pivot element is always the first element (index 0) of the original array (i.e., before any rearranging of elements is performed).
For example, suppose you have an array containing the elements {10, 7, 45, -44, 33, 1}. Here the pivot would be the original first element, which is 10. If I asked you to partition this array, here are some possible results: {7, 1, -44, 10, 45, 33} {-44, 1, 7, 10, 45, 33} {1, 7, -44, 10, 33, 45}
Note that theres more than one valid way to partition the only requirement is that all elements to the left of the pivot are less than the pivot, and all elements to the right of the pivot are greater than or equal to the pivot. Also note that partitioning is NOT the same as sorting the array sorting means to arrange the array elements such that they are all in order from least to greatest. As you can see from the examples above, partitioning alone does not guarantee this. However, partitioning is an important first step in some sorting algorithms!
Consider this algorithm for partitioning an array a:
Create two new arrays called lesser and greater. These will be used to store the elements of a that are less than or greater than the pivot element, respectively.
Loop through all elements of a besides the pivot. If the element is less than the pivot, copy it into the lesser array. If the element is greater than or equal to the pivot, copy it into the greater array.
Create a new array called result, of the same length as a.
Copy the elements of lesser into result.
Copy the pivot element itself into result.
Copy the elements of greater into result.
Return the partitioned array result.
Write a method public static double[] partition(double[] a) that implements this algorithm.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
