Even Odd Seperation Program

Write your question here.
Hello everyone, I have to write a program that separates evens and odds. Like:
Input: 176809
After: 068179
I am stuck and I don't know what to do. Can someone give me another condition for this if condition: if(input[start]%2!=0 && ) and also can someone help me on the whole logic for the program? Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// EvenOddSeperation.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void Seperate (vector<int> & input);
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> input;
	cout<< "Enter the 10 numbers that you want to be seperated";
	for(int x=0;x<10;x++)
	{
		int y;
		cin>> y;
		input.push_back(y);
	}
		
	

	return 0;
}

void Seperate (vector<int> &input)
{
	int size;
	size=input.size();
	int start=0;
	int end=size;
	for(int x=0; x<size; x++)
	{
		if(input[start]%2!=0 && )
		{
		  
		start++;
		end--;
		}
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void separate(std::vector<int> &input)
{
	int first = 0;
	int last = input.size();
    while (true) {
        while ((first != last) && (input[first] % 2 == 0)) {
            ++first;
        }
        if (first == last--) break;
        while ((first != last) && (input[last] % 2 != 0)) {
            --last;
        }
        if (first == last) break;
        std::swap(input[first++], input[last]);
    }
}
Actually whole your separate function can be replaced by single line:
std::partition(input.begin(), input.end(), [](int p){return p % 2 == 0;});
http://ideone.com/6NwlRK
Last edited on
I don't really understand this code I just started programming so can you please give me some code in simpler terms?
It is already as simple as it possible. You cannot probably make it simplier (only arithmetic operations and array access are used)
We are going through array from two sides and swapping elements which are not on their places. Loop will end when beginning and end operations will intersect
Lines 6-8: we are finding first odd element
line 9: check if we finished
Lines 10-12: we are finding last even element
line 9: check if we finished
line 14: swap two elements. You can replace it with
1
2
3
4
int temp = input[first];
input[first] = input[last];
input[last] = temp;
++first;
Wait doesn't lines 6-8 get the even number and lines 10-12 get the odd?
Oh and how would i display the vector?
1
2
3
4
5
6
7
8
9
10
11
12
void separate(std::vector<int> &input)
{
   std::vector<int> even, odd;
   for( auto x : input )
      if( x%2==0 )
         even.push_back(x);
      else
         odd.push_back(x);

   input = even;
   input.insert( input.end(), odd.begin(), odd.end() );
}
O(n) time, O(n) memory
You've "move" each element to its corresponding set, then you concatenate the sets


> Oh and how would i display the vector?
traverse the vector, display the elements.
Last edited on
Topic archived. No new replies allowed.