This library provides a very nice and flexible package of sorting algorithms from which the developer can choose.
|
using System;
using System.Collections;
using System.IO;
namespace NSort
{
/// <summary>
/// http://www.codeproject.com/csharp/csquicksort.asp
/// </summary>
public class QuickSorter : SwapSorter
{
public QuickSorter()
:base()
{}
public QuickSorter(IComparer comparer, ISwap swapper)
:base(comparer,swapper)
{}
/// <summary>
/// Sorts the array.
/// </summary>
/// <param name="array">The array to sort.</param>
public override void Sort(IList array)
{
Sort(array, 0, array.Count-1);
}
public void Sort(IList array, int lower, int upper)
{
// Check for non-base case
if (lower < upper)
{
// Split and sort partitions
int split=Pivot(array, lower, upper);
Sort(array, lower, split-1);
Sort(array, split+1, upper);
}
}
#region Internal
internal int Pivot(IList array, int lower, int upper)
{
// Pivot with first element
int left=lower+1;
object pivot=array[lower];
int right=upper;
// Partition array elements
while (left <= right)
{
// Find item out of place
while ( (left <= right) && (Comparer.Compare(array[left], pivot) <= 0) )
{
++left;
}
while ( (left <= right) && (Comparer.Compare(array[right], pivot) > 0) )
{
--right;
}
// Swap values if necessary
if (left < right)
{
Swapper.Swap(array, left, right);
++left;
--right;
}
}
// Move pivot element
Swapper.Swap(array, lower, right);
return right;
}
#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.
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.