It is necessary to rearrange the even elements at the beginning of the array, and the odd elements at the end.
Please help me write the program code.

Closed due to the fact that the essence of the issue is incomprehensible by the participants: Vladimir Martyanov , Yuri Negometyanov , user194374, VenZell , Pavel Parshin 29 Feb '16 at 8:31 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • @ Mary-Angel, According to the rules of the forum, questions should not be reduced to the decision or the completion of student assignments. Please clarify what you have done yourself and what did not work out. - Nicolas Chabanovsky

7 answers 7

We have an array

a:array[1..20] of integer = (15, 12, 1, 3, 4, 9, 23, 29, 30, 10, 2, 14, 21, 34, 20, 5, 6, 22, 16, 40); 

We process it in a loop:

 k:=1; pos:=0; for i := 1 to 20 do if ((a[i]) mod 2) = 0 then begin pos:=a[i]; a[i]:=a[k]; a[k]:=pos; Inc(k); end; 

After this permutation, we get a sorted array, where the first are the even, the second the odd numbers.

    It is possible with the use of STL.

    Suppose we have an array of int arr of size n

    Insert header

     #include <algorithm> 

    Parity function:

     bool isodd (int num) { return !(num % 2); } 

    Next, we reorganize the array:

     partition (arr, arr+n, isodd); 

    And in C ++ 11 it can be even simpler using lambda functions:

     partition (arr, arr+n, [](int num)->bool {return !(num%2);}); 

    That's all

      In C ++, there is an algorithm std::partition for this.
      It is implemented as follows:

       template<typename ForwardIterator, typename UnaryPredicate> ForwardIterator partition(ForwardIterator first, ForwardIterator last, UnaryPredicate pred) { // ищем первый элемент не удовлятворяющий предикату while (first != last && pred(*first)) ++first; // алгоритм std::find_if_not if (first == last) return first; // все элементы удовлетворяют предикату // first указывает на конец последовательности pred(x) == true for (auto i = std::next(first); i != last; ++i) { if (pred(*i)) { // элемент удовлетворяет предикату // перемещаем его в конец последовательности pred(x) == true std::iter_swap(first, i); ++first; } } return first; } 

      The std::next(i) algorithm returns i+1 for any iterator category , the std::iter_swap effectively reverses the elements pointed to by the iterators.

      As can be seen from the implementation, elements for which the predicate is true retain their relative order, while other elements do not.
      If it is necessary to preserve the relative order, use the stable version of the algorithm, std::stable_partition .

      For even / odd numbers, the predicate and the algorithm call are as follows:

       bool is_odd(int x) { return x % 2 = 0; } int main() { int a[] = {1,2,3,4,5}; std::partition(std::begin(a), std::end(a), is_odd); } 

        What language is the program? The logic is as follows: Run the loop through the array. Check the condition - division of the element by 2: if without a remainder, then we add the even-nth element to the new array with even numbers, if not, then we add to the array with odd numbers. As a result, there will be 2 arrays with even and odd. After that, you just need to merge 2 arrays and you're done)

        • program with ++ - Mary-Angel
        • one
          If you really do, then right. For example, in the spirit of finding a dividing element in quicksort. We move along the array from both sides to the middle. Odd left change (swap) with even right. Algorithm in one pass and in place (no (especially 2 (@Barton!)) Additional arrays). The negative side of this algorithm is the lack of stability (stable) permutation. - avp

        Compose two arrays from the initial array: with even and odd numbers, then derive first even and then odd, the result will be what you want.

          What code? On what? Do psychics today weekend.

          1. You check the element for evenness and change it with the last odd element in its array

            In PHP, you can use the usort() custom sort usort() . which is easy to set up under the reset for elements of different parity and under the sorting for elements of the same parity.

             $issue = [31, 42, 23, 35, 68, 13, 21, 34]; var_dump($issue); usort($issue, function($a, $b){ $sub = ($a & 1) - ($b & 1); if($sub){ return $sub; }else{ return $a - $b; } }); var_dump($issue); 

            Result:

             array (size = 8)
               0 => int 31
               1 => int 42
               2 => int 23
               3 => int 35
               4 => int 68
               5 => int 13
               6 => int 21
               7 => int 34
             array (size = 8)
               0 => int 34
               1 => int 42
               2 => int 68
               3 => int 13
               4 => int 21
               5 => int 23
               6 => int 31
               7 => int 35