-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeparateEvenAndOddNumbers.cpp
More file actions
47 lines (37 loc) · 836 Bytes
/
SeparateEvenAndOddNumbers.cpp
File metadata and controls
47 lines (37 loc) · 836 Bytes
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
41
42
43
44
45
46
47
#include<iostream>
using namespace std;
void swap(int * x, int * y){
int t = * x;
* x = * y;
* y = t;
}
void segregate(int array[], int n){
int left = 0, right = n - 1;
while (left < right){
while (array[left] % 2 == 0 && left < right)
left++;
while (array[right] % 2 == 1 && left < right)
right--;
if (left < right){
swap( & array[left], & array[right]);
left++;
right--;
}
}
}
int main(){
int array[100], n, i;
cout << "Enter number of elements: ";
cin >> n;
cout << "\nEnter elements: ";
for (i = 0; i < n; i++)
cin >> array[i];
cout << "Original array: ";
for (int i = 0; i < n; i++)
cout << array[i] << " ";
segregate(array, n);
cout << "\nArray after divided: ";
for (int i = 0; i < n; i++)
cout << array[i] << " ";
return 0;
}