Question: In the following program, the code for function separate_odd_even is missing. This function should shuffle the numbers in array a such that all odd numbers

In the following program, the code for function separate_odd_even is missing. This function should shuffle the numbers in array a such that all odd numbers appear before even numbers while keeping the orders among odd numbers and orders among even numbers.
Examples:
Example 1:
Input: a=[5,7,6,3,2,1], n=6
Output: a=[5,7,3,1,6,2]
Explanation:
odd numbers 5,7,3,1 are put infront of even numbers 6,2. Please note that the orders among odd numbers and orders among even numbers are unchanged. If your output changes this order, it will be INCORRECT. For example a=[5,7,1,3,6,2] will be an incorrect answer because 5,7,1,3 is a change of order from the original order of 5,7,3,1.
Example 2:
Input: [ 1,2,3,4,5,6,7,8,9,10 ]
Output: [ 1,3,5,7,9,2,4,6,8,10 ]
REQUIREMENTS:
1. The space complexity of your algorithm should be O(1). This means that you are not allowed to allocate another array.
Implement the function on your computer. You are NOT allowed to modify any other parts of the program.
Submit the following:
1. The code inside separate_odd_even function
Grading:
1. There will be 5 test cases.
2. You will get at most 50% grade if you allocated new array even if your output is correct.
3. You will get at most 50% for each test case, if you separated odd and even but didn't keep their orders.
#include
using namespace std;
void separate_odd_even(int a[], int n) {
// Your code here
}
int main() {
int i;
int a[] = { 5,7,6,3,2,1 };
separate_odd_even(a,sizeof(a)/sizeof(int));
for (i=0;i
cout << a[i] << ", ";
}
cout << endl;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!