## Sort vector by even and odd indices. c++

rearrange odd and even values in alternate fashion in ascending order

even numbers at even index and odd numbers at odd index in c

sort even-placed elements in increasing and odd-placed in decreasing order in c

even integers in descending order in c

to print the even index elements in an array

rearrange array such that even positioned are greater than odd

c program to interchange odd and even elements of an array

Is there a one liner (or a simple loop-free) solution to sort a vector by its even and odd indices? Example:

long entries[] = {0,1,2,10,11}; // indices 0 1 2 3 4 std::vector<long> vExample(entries, entries + sizeof(entries) / sizeof(long) ); vExample.sortEvenOdd(vExample.begin(),vExample.end()); // magic one liner I wish existed... for (int i = 0; i < vExample.size(); i++) { std::cout << vExample[i] << " "; }

Now I'd like to have the following output:

0 2 11 1 10 // corresponding to indices 0 2 4 1 3

I tried to do a real *one liner*:

std::stable_partition(std::begin(input), std::end(input), [&input](int const& a){return 0==((&a-&input[0])%2);});

And here is the full program:

#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> input {0,1,2,10,11}; std::stable_partition(std::begin(input), std::end(input), [&input](int const& a){return 0==((&a-&input[0])%2);}); for (auto v : input) std::cout << v << " "; }

Ok I know, it works for the sole reason that vector uses a contiguous array of items and the whole thing is dirty... But for that's a one liner as asked by the OP and it doesn't require anything extra like boost...

**Sort even-placed elements in increasing and odd-placed in ,** The modified array should contain all sorted even-placed numbers followed by Note that the first element is considered as even placed because of its index 0. Sort even-placed elements in increasing and odd-placed in decreasing order We are given an array of n distinct numbers. The task is to sort all even-placed numbers in increasing and odd-placed numbers in decreasing order.

This is not one liner but pretty close:

long entries[] = {0,1,2,10,11}; // indices 0 1 2 3 4 std::vector<long> vExample; for( bool flag : { true, false } ) { auto cond = [&flag]( long ) { flag = !flag; return !flag; }; std::copy_if( std::begin( entries ), std::end( entries ), std::back_inserter( vExample ), cond ); }

**Segregate Even and Odd numbers,** Algorithm: segregateEvenOdd() 1) Initialize two index variables left and right: left C program to segregate even and odd elements of array quick sort method. Thus, once you separate sorting the even and odd elements, the array would look like this: 0 2 4 6 8 1 3 5 7 9. Thus, the methods used to sort even and odd elements in C programming are as follows: Using Standard Method. Read and store the array size into the variable n which is entered by the user.

If you can use Boost, this is pretty concise:

#include <boost/range/adaptor/strided.hpp> #include <boost/range/adaptor/sliced.hpp> #include <boost/range/algorithm_ext/push_back.hpp> #include <iostream> #include <vector> int main() { using namespace boost::adaptors; std::vector<int> input {0,1,2,10,11}; std::vector<int> partitioned; boost::push_back(partitioned, input | strided(2)); boost::push_back(partitioned, input | sliced(1, input.size()) | strided(2)); for (auto v : partitioned) std::cout << v << " "; }

You can of course wrap that in a function to get a one liner in the calling code. Live

**C Program To Sort Even And Odd Elements Of Array,** b) If a[i] is odd number,if j<n then place the odd number at b[j++]. 6) After 5th step, the array b[] contains even numbers from the index 0 to n-c-1 and odd numbers Given a vector, keep track of the present indexes corresponding to each element and after sorting print element with its previous respective indexes. Examples: Input: Arr[] = {2, 5, 3, 7, 1}

I don't like the messy business of fiddling with the addresses that the accepted answer of @fjardon proposes. @Slava's suggestion is much better and combined with the OP's code gives something that works quite well:

int main() { std::vector<int> vals {0,2,3,-3,8,-5,7,8}; bool flag = true; std::stable_partition(begin(vals), end(vals), [&flag] (auto el) mutable { // toggle flag, return previous value flag = !flag; return !flag; }); for (auto v : vals) std::cout << v << " "; } Output: 0 3 8 7 2 -3 -5 8

**Shuffle a sorted array so that left portions contains even indices and ,** Shuffle a sorted array so that left portions contains even indices and right portion contains odd indices numbers · c++ algorithm c++11 sorting Even numbers at even index and odd numbers at odd index Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index.

What you need is stable_partition. Define a predicate which checks whether the index is even using modulo 2, and you are good to go.

**Odd–even sort,** In computing, an odd–even sort or odd–even transposition sort is a relatively simple sorting Data structure, Array Here a zero-based index is assumed: function S.; Dhall, S. K. & Miller, L. L. (1984), Alt, Franz L. & Yovits, Marshall C. (eds.) C program to put all even and odd elements of array in two separate array. C program to count total even and odd elements in a given array. C program to search an element in the array. C program to count frequency of each element of the array. C program to merge two array in third array.

**Essential Mathematics for NMR and MRI Spectroscopists,** Keith C. Brown First, we sort the even and odd members of the time-domain vector using a sorting matrix: || || | | || |o] }; ; ;|. f(3) transform step using the W2 matrix (the Fourier transform 2×2 the k-indices were incremented only over 0 and 1. I am trying to sort an array of 1xN in different odd and even arrays till the length last array =4 for example: X=1:32; N=length(X) m=log2(N);

**C Program to Print the Number of Odd & Even Numbers in an Array ,** Iterator is used to reach out every position of the array, scanning the particular array element and checking whether it is divisible by 2 or not, thus sorting even and Python | Separate odd and even index elements Python list are quite popular and no matter what type of field one is coding, one has to deal with lists and its various applications. In this particular article, we discuss ways to separate odd and even indexed elements and its reconstruction join.

**how to separate odd and even elements of a matrix with out using ,** The first one contains all the even elements of A and nothing else, while the i am using this approach, where r vector contains the row position of odd elemets and c vector contains the now dont know how to make a vector conatining all the odd and even elements. [Aodd,Aeven] = sort(A); %calling the local func. % concatenate odd and even row indices, then use SORT to find appropriate reordering [~,reorderI] = sort([oddI,evenI]) % reorder matrix to reconstruct original matrix.

##### Comments

- use
`std::end(entries)`

instead of long expression calculating it - this also works:
`std::vector<long> vExample{0,1,2,10,11}`

- If you can use Boost,
`boost.strided`

is all you need. That way you would not even have to actually reorder the vector, which might help performance. - By the way, you can do
`[flag = true]`

and it will work with`mutable`

AFAIK. - FWIW you could also omit the parameter name and just leave
`(auto)`

or the compiler might complain about an unused variable. Otherwise`[[maybe_unused]]`

would work. - Last thing which is more of a preference, I'd rather count and check for even-ness than setting a flag to it. This gives me this lambda function:
`[index = 0](auto) mutable { return index++ % 2 == 0; }`

`stable_partition`

works by doing swaps; would it become confused when the index of an element changes?- Can you give an example of this predicate?
- This answer is quite incomplete, it is not at all clear how to do this with
`stable_partition`

. - Most important how to proof that
`std::stable_partition`

would work properly as just a test case or two is not enough. For example if it cannot allocate additional buffer behavior would change. - @BenjaminLindley see my answer for the predicate. I am not proud of it but for the sake of the challenge I did it ...