几种常见的排序算法

  • 交换两个对象(排序中会用到)
private static void Interop_ExchangeValue<T>(ref T m, ref T n)
{
    T Temp = m;
    m = n;
    n = Temp;
}

插入排序

public static void InsertSort<T>(ref T[] array, IComparer<T> comparer)
{
    if (array.Length > 0)
    {
        for (int i = 1; i < array.Length; i++)
        {
            int front = i - 1;
            int index = i;
            while (front >= 0)
            {
                if (comparer.Compare(array[index], array[front]) < 0)
                {
                    Interop_ExchangeValue(ref array[index], ref array[front]);
                    index = front;
                }
                front--;
            }
        }
    }
}

选择排序

public static void SelectSort<T>(ref T[] array, IComparer<T> comparer)
{
    if (array.Length > 0)
    {
        for (int i = 0; i < array.Length; i++)
        {
            T minValue = array[i];
            int minIndex = i;
            for (int j = i; j < array.Length; j++)
            {
                if (comparer.Compare(minValue, array[j]) > 0)
                {
                    minValue = array[j];
                    minIndex = j;
                }
            }
            Interop_ExchangeValue(ref array[i], ref array[minIndex]);
        }
    }
}

冒泡排序

public static void BubbleSort<T>(ref T[] array, IComparer<T> comparer)
{
    if (array.Length > 0)
    {
        for (int i = 0; i < array.Length - 1; i++)
        {
            for (int j = array.Length - 1; j > i; j--)
            {
                if (comparer.Compare(array[j - 1], array[j]) > 0)
                {
                    Interop_ExchangeValue(ref array[j - 1], ref array[j]);
                }
            }
        }
    }
}

双向冒泡排序

public static void BidirectionalBubleSort<T>(ref T[] array, IComparer<T> comparer)
{
    if (array.Length > 0)
    {
        int end = array.Length;
        int start = -1;
        bool swapped = false;
        do
        {
            swapped = false;
            start++;
            end--;

            for (int j = start; j < end; j++)
            {
                if (comparer.Compare(array[j], array[j + 1]) > 0)
                {
                    Interop_ExchangeValue(ref array[j], ref array[j + 1]);
                    swapped = true;
                }
            }
            for (int j = end - 1; j >= start; j--)
            {
                if (comparer.Compare(array[j], array[j + 1]) > 0)
                {
                    Interop_ExchangeValue(ref array[j], ref array[j + 1]);
                    swapped = true;
                }
            }
        } while (start < end && swapped);
    }
}

快速排序

public static void QuickSort<T>(ref T[] array, IComparer<T> comparer)
{
    if (array.Length > 0)
    {
        Interop_QuickSort<T>(ref array, 0, array.Length - 1, comparer);
    }
}

private static void Interop_QuickSort<T>(ref T[] array, int left, int right, IComparer<T> comparer)
{
    if (left < right)
    {
        T key = array[(left + right) / 2];
        int i = left - 1;
        int j = right + 1;
        while (true)
        {
            while (comparer.Compare(array[++i], key) < 0) ;
            while (comparer.Compare(array[--j], key) > 0) ;
            if (i >= j)
            {
                break;
            }
            Interop_ExchangeValue(ref array[i], ref array[j]);
        }
        Interop_QuickSort(ref array, left, i - 1, comparer);
        Interop_QuickSort(ref array, j + 1, right, comparer);
    }
}

堆排序

public static void HeapSort<T>(ref T[] array, IComparer<T> comparer)
{
    for (int i = (array.Length - 1) / 2; i >= 0; i--)
    {
        Interop_HeapAdjust(ref array, i, array.Length - 1, comparer);
    }
    for (int i = array.Length - 1; i >= 1; i--)
    {
        Interop_ExchangeValue<T>(ref array[0], ref array[i]);
        Interop_HeapAdjust(ref array, 0, i - 1, comparer);
    }
}

private static void Interop_HeapAdjust<T>(ref T[] array, int index, int length, IComparer<T> comparer)
{
    T Temp = array[index];
    int j = index * 2 + 1;
    while (j <= length)
    {
        if (j < length)
        {
            if (comparer.Compare(array[j], array[j + 1]) < 0)
            {
                j = j + 1;
            }
        }
        if (comparer.Compare(Temp, array[j]) < 0)
        {
            array[index] = array[j];
            index = j;
            j = 2 * index + 1;
        }
        else
        {
            j = length + 1;
        }
    }
    array[index] = Temp;
}

在排序的时候,需要实现IComparer接口,可以用在合适的地方。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值