-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bucket.cs
42 lines (37 loc) · 1.04 KB
/
Bucket.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Nutshell.Sorting
{
public static class Bucket
{
public static IEnumerable<int> Sort(IEnumerable<int> values, int min, int max)
{
// create buckets
var nBuckets = max - min + 1;
var buckets = new List<List<int>>();
for (int i = 0; i < nBuckets; i++)
{
buckets.Add(new List<int>());
}
// add values to the buckets
foreach (var value in values)
{
var key = Hash(value, min);
buckets[key].Add(value);
}
// join buckets into a single list
var returnValues = new List<int>();
foreach (var bucket in buckets)
{
returnValues.AddRange(bucket);
}
return returnValues;
}
private static int Hash(int value, int min)
{
return value - min;
}
}
}