-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeparateEvenAndOddNumbers.cs
More file actions
43 lines (41 loc) · 1.11 KB
/
SeparateEvenAndOddNumbers.cs
File metadata and controls
43 lines (41 loc) · 1.11 KB
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
namespace DA.Algorithms.Problems
{
public static class SeparateEvenAndOddNumbers
{
/// <summary>
/// Separate even numbers from the odd numbers in the collection.
/// </summary>
/// <param name="array">A collection with even and odd numbers.</param>
public static void Separate (int[] array)
{
int left = 0;
int right = array.Length - 1;
while (left < right)
{
if (array[left] % 2 == 0)
{
++left;
}
else if (array[right] % 2 == 1)
{
--right;
}
else
{
Swap (ref array[left], ref array[right]);
++left;
--right;
}
}
}
/// <summary>
/// Swap two elements.
/// </summary>
public static void Swap<T> (ref T first, ref T second)
{
T temp = first;
first = second;
second = temp;
}
}
}