This repository was archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
202 lines (179 loc) · 5.57 KB
/
Program.cs
File metadata and controls
202 lines (179 loc) · 5.57 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace C_
{
class Program {
static Dictionary<int, int> threadsIndices;
static int[] initialArray;
static int numOfThreads = 8;
static void Main(string[] args)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
int sizeOfArray = 10000;
initialArray = new int[sizeOfArray];
string fileContent = File.ReadAllText(sizeOfArray.ToString() + ".txt");
string[] integerStrings = fileContent.Split(new char[] { ',', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < sizeOfArray; i++)
initialArray[i] = int.Parse(integerStrings[i]);
int maxNum = findMaxNum();
calculateIndicesForThreads();
radixParallel(maxNum, sizeOfArray);
// radixSequence(initialArray, sizeOfArray, maxNum);
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("Elapsed time: " + elapsedMs.ToString() + "ms");
}
static int findMaxNum()
{
return initialArray.Max();
}
static void generateArray(int Min, int Max, int sizeOfArray)
{
Random randNum = new Random();
for (int i = 0; i < sizeOfArray; i++)
{
initialArray[i] = randNum.Next(Min, Max);
}
}
static void radixParallel(int maxNum, int sizeOfArray)
{
var list = new List<int>(numOfThreads);
for (var i = 0; i < numOfThreads; i++) list.Add(i);
for (int rank = 1; maxNum / rank > 0; rank *= 10)
{
using (var countdownEvent = new CountdownEvent(numOfThreads))
{
int i = 0;
foreach (var indicesPair in threadsIndices)
{
ThreadPool.QueueUserWorkItem(state =>
{
countingsort(indicesPair.Key, indicesPair.Value, rank);
countdownEvent.Signal();
}, list[i]);
i++;
}
countdownEvent.Wait();
reorderBuckets(rank, sizeOfArray);
}
}
}
static void calculateIndicesForThreads()
{
threadsIndices = new Dictionary<int, int>();
var len = (int)initialArray.Length / numOfThreads;
int iterator = 0;
int numRemaining = initialArray.Length - len * numOfThreads;
for (int i = 0; i < numOfThreads; i++)
{
if (numRemaining > 0)
{
threadsIndices[iterator] = iterator + len + 1;
iterator += len + 1;
numRemaining--;
}
else
{
threadsIndices[iterator] = iterator + len;
iterator += len;
}
}
}
static void countingsort(int startIndex, int endIndex, int place)
{
int[] output = new int[endIndex - startIndex];
//range of the number is 0-9 for each place considered.
int[] freq = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
//count number of occurrences in freq array
for (int i = startIndex; i < endIndex; i++)
{
freq[(initialArray[i] / place) % 10]++;
}
//Change count[i] so that count[i] now contains actual
//position of this digit in output[]
for (int i = 1; i < 10; i++)
{
freq[i] += freq[i - 1];
}
//Build the output array
for (int i = endIndex - 1; i >= startIndex; i--)
{
output[freq[(initialArray[i] / place) % 10] - 1] = initialArray[i];
freq[(initialArray[i] / place) % 10]--;
}
//Copy the output array to the input Array, Now the Array will
//contain sorted array based on digit at specified place
for (int i = startIndex; i < endIndex; i++)
initialArray[i] = output[i - startIndex];
}
static int getDigitInNumberByRank(int number, int rank)
{
int counter = 1;
while (number > 0 && rank != counter)
{
number = number / 10;
counter *= 10;
}
return number % 10;
}
static void reorderBuckets(int rank, int sizeOfArray)
{
int pos = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < sizeOfArray; j++)
{
int currentNumber = initialArray[j];
if (getDigitInNumberByRank(currentNumber, rank) == i)
{
initialArray[j] = initialArray[pos];
initialArray[pos] = currentNumber;
pos++;
}
}
}
}
static void PrintArray()
{
int n = initialArray.Length;
for (int i = 0; i < n; i++)
Console.Write(initialArray[i] + " ");
Console.Write("\n\n");
}
public static void countSort(int[] arr, int n, int exp)
{
int[] output = new int[n]; // output array
int i;
int[] count = new int[10];
// initializing all elements of count to 0
for (i = 0; i < 10; i++)
count[i] = 0;
// Store count of occurrences in count[]
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
// Change count[i] so that count[i] now contains
// actual
// position of this digit in output[]
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
// Build the output array
for (i = n - 1; i >= 0; i--)
{
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to current
// digit
for (i = 0; i < n; i++)
arr[i] = output[i];
}
public static void radixSequence(int[] arr, int n, int max)
{
for (int exp = 1; max / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
}
}