|
using System;
using System.Collections.Generic;
using System.Text;
namespace DataStructuresDotNet.Sorting
{
public sealed class BucketSorter : Sorter<int>
{
#region Globals
private int max;
#endregion
#region Construction
public BucketSorter(int maxUniverse)
{
this.max = maxUniverse;
}
#endregion
#region Sorter<T> Members
public override void Sort(IList<int> list, IComparer<int> comparer)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
if (comparer == null)
{
throw new ArgumentNullException("comparer");
}
if (list.Count <= 1)
{
return;
}
int maxIndex = max + 1;
int[] counters = new int[maxIndex];
for (int i = 0; i < list.Count; i++)
{
counters[list[i]]++;
}
int position = 0;
for (int i = 0; i < maxIndex; i++)
{
for (int j = 0; j < counters[i]; j++)
{
list[position] = i;
position++;
}
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
The author is a software consultant in South Africa, specializing in bespoke software solutions.