Reputation: 11
I'm trying to shuffle array.
{10, 64, 79, 6, 13, -14, 1, 0, 66, 2}
It should do it when elements are smaller than 3 should be put on the left side and bigger than 3 on the right side
{-14 1, 0, 2, 10, 64, 79, 6, 13, 66}
int indexes[1000], c;
for(int i = 0; i < n; i++)
{
if(array[i] < 3)
{
indexes[c] = array[i];
}
}
Upvotes: 1
Views: 50
Reputation: 60308
Based on your expected output, it looks like you want to preserve the original relative order of the elements. You can use the std::stable_partition algorithm for that:
std::stable_partition(array, array + n, [](int i) { return i < 3; });
If you don't care about the relative order within a partition, you can just use std::partition
.
Note that both these algorithms will also return the "partition point" which allows you to tell how many elements were moved to the beginning of the range.
Upvotes: 3