-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionZeroOne.cs
More file actions
120 lines (113 loc) · 4.12 KB
/
PartitionZeroOne.cs
File metadata and controls
120 lines (113 loc) · 4.12 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
namespace DA.Algorithms.Problems
{
public static class PartitionZeroOne
{
/// <summary>
/// Sort array so that 0s come first followed by 1s.
/// Return the minimum number of swaps required to sort the array.
/// <para>Time Complexity - O(n)</para>
/// </summary>
/// <param name="array">Collection containing 0s and 1s</param>
public static int Partition01 (int[] array)
{
int leftIndex = 0;
int rightIndex = array.Length - 1;
int swapCounter = 0;
while (leftIndex < rightIndex)
{
// Finding value 1 at front of the collection
while (array[leftIndex] == 0)
{
leftIndex++;
}
// Finding value 0 at back of the collection
while (array[rightIndex] == 1)
{
rightIndex--;
}
// Swap found values
if (leftIndex < rightIndex)
{
Swap (ref array[leftIndex], ref array[rightIndex]);
swapCounter++;
}
}
return swapCounter;
}
/// <summary>
/// Sort array so that 0s come first followed by 1s and then 2s in the end.
/// <para>Time Complexity - O(n)</para>
/// </summary>
/// <param name="array">Collection containing 0s and 1s</param>
public static void Partition012 (int[] array)
{
int leftIndex = 0;
int rightIndex = array.Length - 1;
int currentIndex = 0;
while (currentIndex <= rightIndex)
{
// If value is 0 then swap to front of the collection
if (array[currentIndex] == 0)
{
Swap (ref array[currentIndex], ref array[leftIndex]);
currentIndex++;
leftIndex++;
}
// If value is 2 then swap to back of the collection
else if (array[currentIndex] == 2)
{
Swap (ref array[currentIndex], ref array[rightIndex]);
rightIndex--;
}
// If value is 1 then just go to next index
else
{
currentIndex++;
}
}
}
/// <summary>
/// Sort array so that values smaller than range come to left, then values under the range followed with values greater than the range.
/// <para>Time Complexity - O(n)</para>
/// <see href="https://www.geeksforgeeks.org/three-way-partitioning-of-an-array-around-a-given-range/"/>
/// </summary>
/// <param name="array">Collection containing an elements</param>
public static void PartitionRange (int[] array, int minRange, int maxRange)
{
int startIndex = 0;
int endIndex = array.Length - 1;
int currentIndex = 0;
while (currentIndex < endIndex)
{
// If value is smaller than range, then swap to front of the collection
if (array[currentIndex] < minRange)
{
Swap (ref array[currentIndex], ref array[startIndex]);
currentIndex++;
startIndex++;
}
// If value is greater than range, then swap to back of the collection
else if (array[currentIndex] > maxRange)
{
Swap (ref array[currentIndex], ref array[endIndex]);
endIndex--;
}
// Skip current value and go to next index
else
{
currentIndex++;
}
}
}
/// <summary>
/// Swap two elements.
/// </summary>
public static void Swap<T> (ref T first, ref T second)
{
T temp = first;
first = second;
second = temp;
}
}
}