1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
pickupchik [31]
3 years ago
15

Write a method called evenBeforeOdd that accepts an array of integers and rearranges its elements so that all even values appear

before all odds. For example, if the array is [5, 4, 2, 11, 9, 10, 4, 7, 3], then after the method has been called, one acceptable ordering of the elements would be [4, 2, 10, 4, 5, 11, 9, 7, 3]. The exact order of the elements does not matter, so long as all even values appear before all odd values. The array might contain no even elements or no odd elements. Do not use any temporary arrays in your solution, and do not call Arrays.sort.

Computers and Technology
1 answer:
lara31 [8.8K]3 years ago
7 0

Answer:

I am writing a JAVA program.

public class EvenOdd  

// function that rearranges array elements so that even values come before odd values  

{   static void evenBeforeOdd (int array[])      {

/* i and j are two array indices and i is positioned at the start of the array and j is positioned at last element of array */

       int i = 0, j = array.length - 1;

       while (i < j)   { // loop continues till value of i is less than that of j

           while (array[i]%2 == 0 && i < j) //array element at i is even no.

               i = i + 1;    // increments index i by one

           while (array[j]%2 == 1 && i < j)  // array element at j-th position is odd

               j = j - 1;     //decrements j index by 1

           if (i < j)  { if i is less than j swap their elements

               int temp = array[i];  

               array[i] = array[j];  

               array[j] = temp;  

               i++;  

               j--;              }          }      }    

   public static void main (String[] args)  

   {   int array[] = {5, 4, 2, 11, 9, 10, 7, 3};  

// declares an array and assign it values

       evenBeforeOdd(array);   // calls evenBeforeOdd() function  

       for (int i = 0; i < array.length; i++)

 //traverses through array using loop to print elements of the array with even elements before odd

           System.out.print(array[i]+"\n");      }  }

   

Explanation:  

  • This program has a method evenBeforeOdd() that takes an array of integers as parameter and rearranges the elements of the array in such a way that the even values appear before odd values.
  • The method has two index variables i and j where i starts from the first element of the array which means it is positioned at the left of the array and j is positioned at the last element of the array which is 1 minus the length of the array. This means j is placed at the right of the array.
  • The first while loop keeps executing till i is less than j.
  • The second while loop keeps checking if the element at i-th index is even. This is checked by taking mod of the element at i index  with 2. If the result of the mod is equal to 0 this means that the element is completely divisible by 2 so its an even number. Then the value of i is incremented by 1.
  • The third while loop contains j index which moves through the array from right and checks for the odd elements. This is checked by taking mod of the element at j index  with 2. If the result of the mod is equal to 1 this means that the element is not completely divisible by 2 so its an odd number. Then the value of j  is decremented by 1.
  • So loops keeps incrementing i index until an odd number is seen and keeps decrementing j index until even number is seen.
  • If i is less than j then array element at i-th index is swapped by array element at j-th index.
  • Finally the main() function calls this method evenBeforeOdd () and passes an array containing 5, 4, 2, 11, 9, 10, 4, 7, 3 to display even values before odd values in the output.
  • The code along with the output is attached.

You might be interested in
What are your thoughts on copyright?<br><br> (Write 2 or more sentences)
victus00 [196]

Answer:

I look down on copyright. To take someone else's work and disguising it as your own seems like a form of theft.

5 0
3 years ago
Read 2 more answers
Write a MATLAB code for the following problem:
AysviL [449]
<span>Here is matlab that should work % cos(x) = 1 - (x^2)/2! + (x^4)/4! -(x^6)/6!+(x^8)/8!... % let y= x*x % cos(x) = sum( (-y)^n/(2n)! ) format short x= 0.3*pi; y= x*x; for N= 1:6 n= 0:N; s1= [(-y).^n./factorial(2*n) ] mac= sum(s1); cx= cos(x); str= sprintf('%d terms. series: %12.10f cos(x): %12.10f\n %12.10f',... N, mac,cx, (cx-mac)); disp(str); end;</span>
7 0
3 years ago
The algorithm ____ is used to find the elements in one range of elements that do not appear in another range of elements.
Musya8 [376]

Answer:

A. set_union

Explanation:

The algorithm set_union is used to find the elements in one range of elements that do not appear in another range of elements.

4 0
3 years ago
What sequence would a user need to follow to change Chart A into Chart B?
alexandr402 [8]

Either options C or D

You can change the chart type of the whole chart or for single data series in most 2-D charts to give the chart a different look. In 3D or bubble charts, you can only change the chart type of the whole chart. By following the steps in the answers above, you will be in a position to select available chart type like column chart or line chart.

3 0
3 years ago
They convert energy from the Sun into usable chemical energy by the process of photosynthesis. They are
Alchen [17]
Photosynthetic organisms are producers because they are producing chemical energy useable by living things.
4 0
3 years ago
Other questions:
  • Put the following five steps in the order in which you would perform them to use the Paste Special function: ____. 1. Select and
    5·1 answer
  • Which of the following statements comparing debit cards to credit cards is TRUE?
    13·2 answers
  • Importance of professional education​
    15·1 answer
  • Write a program with a car's miles/gallon and gas dollars/gallon (both floats) as input, and output the gas cost for 10 miles, 5
    15·1 answer
  • You are designing an ecommerce web application that will scale to hundreds of thousands of concurrent users. Which database tech
    10·1 answer
  • Enthusiasm codehs 7.6.4 Write a function called add_enthusiasm that takes a string and returns that string in all uppercase with
    15·1 answer
  • What are 5 different google g-suite tools that you can use to collaborate and communicate with othe
    15·1 answer
  • You have learned a lot about the types of careers that are involved with building a playground. Create two job descriptions of p
    7·2 answers
  • Cómo surge y cuál es el objetivo del observatorio de radio y televisión
    11·1 answer
  • The science of networking is attributed to which government's involvement?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!