C# quick sort

Following class provide a method to quick sort an array.

public interface ISwap
{
void Swap(ArrayList array, int left, int right);
}
public class Sort : ISwap, IComparer
{
public static IComparer comparer;
public static ISwap swapper;
public static void QuickSort(ArrayList array)
{
Sort s = new Sort();
Sort.comparer = s;
Sort.swapper = s;
QuickSort(array, 0, array.Count - 1);
}
public static void QuickSort(ArrayList array, ISwap swapper)
{
Sort.comparer = new Sort();
Sort.swapper = swapper;
QuickSort(array, 0, array.Count - 1);
}
public static void QuickSort(ArrayList array, IComparer comparer)
{
Sort.comparer = comparer;
Sort.swapper = new Sort();
QuickSort(array, 0, array.Count - 1);
}
public static void QuickSort(ArrayList array, IComparer comparer, ISwap swapper)
{
Sort.comparer = comparer;
Sort.swapper = swapper;
QuickSort(array, 0, array.Count - 1);
}
private static void QuickSort(ArrayList array, int lower, int upper)
{
if (lower < upper)
{
int split = Pivot(array, lower, upper);
QuickSort(array, lower, split - 1);
QuickSort(array, split + 1, upper);
}
}
private static int Pivot(ArrayList array, int lower, int upper)
{
int left = lower + 1;
object pivot = array[lower];
int right = upper;
while (left <= right)
{
while ((left <= right) && (comparer.Compare(array[left], pivot) <= 0))
{
++left;
}
while ((left <= right) && (comparer.Compare(array[right], pivot) > 0))
{
--right;
}
if (left < right)
{
swapper.Swap(array, left, right);
++left;
--right;
}
}
swapper.Swap(array, lower, right);
return right;
}
public void Swap(ArrayList array, int left, int right)
{
object swap = array[left];
array[left] = array[right];
array[right] = swap;
}
public int Compare(object a, object b)
{
return a.ToString().CompareTo(b.ToString());
}
}


endmemo.com © 2024  | Terms of Use | Privacy | Home