-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCS_QuickSort.cs
More file actions
195 lines (186 loc) · 5.94 KB
/
CS_QuickSort.cs
File metadata and controls
195 lines (186 loc) · 5.94 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace QuickSort
{
public class ShortState
{
public int fstIndex, lstIndex, eqlCount;
public ShortState(int fstIndex, int lstIndex)
{
this.fstIndex = fstIndex;
this.lstIndex = lstIndex;
}
}
public class ShortSorter
{
protected short[] data;
protected ConcurrentQueue<ShortState> works;
public CountdownEvent cde;
public ShortSorter(short[] source)
{
data = source;
}
public void Randomize(short max_exclusive)
{
Random rnd = new Random();
for (int i = 0; i < data.Length; i++)
{
data[i] = (short)rnd.Next(max_exclusive);
}
}
public bool CheckSorted()
{
short curr = data[0];
bool done = true;
for (int i = 1; i < data.Length; i++)
{
if (data[i] < curr)
{
Console.WriteLine($"Break on i={i}, val={data[i]}, curr={curr}");
done = false;
break;
}
else
if (curr < data[i]) curr = data[i];
}
Console.WriteLine($"Curr is {curr}, sort is {done}. Length={data.Length}");
return done;
}
//entry
public void LoopSort()
{
works = new ConcurrentQueue<ShortState>();
works.Enqueue(new ShortState(0, data.Length - 1));
cde = new CountdownEvent(1);
ThreadPool.QueueUserWorkItem(DoWorkLoop);
}
//splitting data
private int DoPartByPivot(ShortState work)
{
int eqlCount = 0, fstIndex = work.fstIndex, lstIndex = work.lstIndex;
short value, pivot = data[fstIndex++];
// iterate through comparing with pivot
while (fstIndex <= lstIndex)
{
value = data[fstIndex];
// look for smaller or equal to the pivot
if (value <= pivot)
{
// increment the index
fstIndex++;
}
else
{
// move greater to the right side
SwapValues(fstIndex, lstIndex);
lstIndex--;
}
}
fstIndex = work.fstIndex;
// iterate through comparing with pivot
while (fstIndex <= lstIndex)
{
value = data[fstIndex];
// look for equal to the pivot
if (value == pivot)
{
// move equal to the right side
SwapValues(fstIndex, lstIndex);
lstIndex--;
eqlCount++;
}
else
{
// increment the index
fstIndex++;
}
}
//count of repeated values
work.eqlCount = eqlCount;
//values greater or equal to the pivot
return fstIndex;
}
//worker`s loop
private void DoWorkLoop(Object o)
{
try
{
while (works.TryDequeue(out ShortState work))
{
DoLoopSort(work);
}
}
finally
{
// workers done
cde.Signal();
}
}
//try add new worker
private void TryAddLoop()
{
if (cde.CurrentCount < 7)
{
ThreadPool.QueueUserWorkItem(DoWorkLoop);
cde.AddCount();
// workers added
//Console.WriteLine("worker added.");
}
}
//quick sort algorithm
private void DoLoopSort(ShortState work)
{
//work begin
int pivotIndex = DoPartByPivot(work);
int fstIndex = work.fstIndex;
int lstIndex = work.lstIndex;
int eqlCount = work.eqlCount;
// recurse on the left block
if (fstIndex < pivotIndex - 1)
{
works.Enqueue(new ShortState(fstIndex, pivotIndex - 1));
//do in current loop
}
// recurse on the right block
if (pivotIndex + eqlCount < lstIndex)
{
works.Enqueue(new ShortState(pivotIndex + eqlCount, lstIndex));
TryAddLoop();
}
//work done
}
internal void SwapValues(int firstIndex, int secondIndex)
{
//if (firstIndex == secondIndex) return;
short holder = data[firstIndex];
data[firstIndex] = data[secondIndex];
data[secondIndex] = holder;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Work 4096 * 1024 started");
short[] data = new short[4096 * 1024];
ShortSorter shortSorter = new ShortSorter(data);
shortSorter.Randomize(256);
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
//71 - 78 ms
shortSorter.LoopSort();
shortSorter.cde.Wait(600);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Console.WriteLine("Done: Время {0} ms", ts.Milliseconds);
//Thread.Sleep(400);
shortSorter.CheckSorted();
Console.Read();
}
}
}