algorithm - How to Sort given Array in sorted odd and Sorted Even in same array In C++? -
the given array
a[]={200,100,25,67,3,56,10,78,7,11}
the expected output
a[] ={3,7,11,25,67,10,56,78,100,200}
where first 5 numbers odd numbers in ascending order, , next 5 numbers in ascending order.
if knew algorithm please share here.
consider std::vector, need provide custom comparer std::sort indicating odds "less" numbers:
auto odd_even_comparer =[](int a, int b){ if( % 2 == b % 2 ) return < b; else if(a % 2 == 1 && b % 2 == 0 ) return true; else if(a % 2 == 0 && b % 2 == 1 ) return false; // in case return a<b; }
and make call:
std::sort(a.begin(), a.end(), odd_even_comparer);
output: 3, 7, 11, 25, 67, 10, 56, 78, 100, 200
Comments
Post a Comment