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
Luis saves an attachment that he received from Kevin. Where will the attachment save by default?
tia_tia [17]

Answer:

first one I do believe good luck with the answer

6 0
3 years ago
Help me
mestny [16]

Answer:

  1. True
  2. False
  3. False
  4. True
  5. False

Explanation:

mark me as brainlyest

6 0
3 years ago
5 band resistor with two red bands, two gold bands, one purple band?
Mamont248 [21]

According to below picture, two gold bands on a 5 band resistor cannot happen. What is the order of the bands?

8 0
3 years ago
What will the document.write() statement display, given the following code snippet? var scores = new Array[94, 90, 88, 82, 73, 7
astra-53 [7]

Answer:

There are 7 scores recorded. The dropped score is 75

Explanation:

The code snippet above prints through the document.write() function. the output prints the string serving as the argument, and makes use of variables (lgth) and expression also (scores [lgth -1]). what it does is. From the point where it sees +lgth+, it checks the value, which is 7. And when it sees scores [lgth-1] it evaluates it to 75. Hence, the string above is produced.

4 0
3 years ago
What is required to display content on transparencies?
VladimirAG [237]
An overhead projector is required to display content on transparencies. A transparency is a thin sheet of plastic in which light is reflected upon to display content.The light source emits light towards the transparency and then the image is displayed on the wall. Usually, this light source comes from below and lights upward and so the content is depicted above or overhead.
6 0
3 years ago
Other questions:
  • How do you train a computer to recognize your voice?
    14·2 answers
  • What company handles security for a lot of the e-commerce on the internet and also maintains two root name server clusters?
    8·1 answer
  • Inkjet printers and laser printers are examples of ________ printers. (1 point)
    13·1 answer
  • 4. How can you select non-adjacent cells (i.e. cells that are not all together in one block)?
    5·2 answers
  • When you leave your computer for a short period of time, why should you put your computer into Standby mode?
    5·2 answers
  • 16. A
    15·1 answer
  • What is payload?
    8·1 answer
  • What solicits online input such as product ratings from consumers?
    7·1 answer
  • what stage is the most inner part of the web architecture where data such as, customer names, address, account numbers, and cred
    11·1 answer
  • List three natural defects of wood<br>​
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!