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
How to change the indent of the list item "regular" on slide 2 to .5 inches in powerpoint?
galina1969 [7]
<span>You can change the identation of text in Powerpoint by right clicking on the list item and selecting, "Format Text". On the paragraph tab, you can adjust the indentation before text as given in inches.</span>
4 0
3 years ago
Match the definition
Katen [24]

Answer:

¿???????????? SORRY..........

8 0
3 years ago
What is APR?
omeli [17]

Answer:

APR is the Interest rate advertised by lenders.

Explanation:

APR is the annual percentage rate that is different from the interest rate. Interest rate is the rate of borrowing money. While APR is the rate which includes interest rate, processing fee and other cost that are involved in loan approving.

This cost is decided by lender, which may be equal to interest rate or greater than interest rate.

5 0
3 years ago
Drag each tile to the correct box.
kari74 [83]

Answer:

RESET...

Explanation:

.The file is not Indexed for searches.

.The file can only be viewed.

.The file is ready for backup.

.The file or folder is hidden.

√ Reset

.Next

6 0
3 years ago
A film company want to video animals in the wild acting naturally. They decide to
erik [133]

Answer:

y

Explanation:

g

5 0
2 years ago
Other questions:
  • Which of the following is an example of a consumer service? computer builder motorcycle manufacturer cabinet maker air condition
    15·2 answers
  • What are the pasting options in Outlook 2016? Check all that apply.
    10·2 answers
  • What is the theory of trouble shooting
    10·2 answers
  • The primary benefit of a VPN that uses _________ is that an intercepted packet reveals nothing about the true destination system
    6·1 answer
  • What is an orthochromatic film?
    12·1 answer
  • Give a recursive algorithm that takes as input a string s, removes the blank characters and reverses the string. For example, on
    7·1 answer
  • What best describes "broadband access"?
    13·1 answer
  • You are writing code to store the length of a side of a square. Which one is a good variable name
    9·1 answer
  • Wendell notices that the company's top executives share a belief that managers are directly responsible for the organization's s
    13·1 answer
  • A(n) ____________________ key is a key that is not reused, but rather is only used once, thus improving security by reducing the
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!