我想要一个有效的排序算法来排序数组

问题描述 投票:0回答:3
for (int i = 1; i < data.Count; i++) 
{
    int j = i;
    while (j > 0)
    {
        if (numarray[j - 1] > numarray[j])
        {   
            int temp = numarray[j - 1];
            numarray[j - 1] = numarray[j];
            numarray[j] = temp;
            j--;

        }

        else
            break;
    }
}

有人可以帮我确定上述代码的排序算法是什么?我知道冒泡排序效率不高。如果我要使用插入排序算法,我该如何改进上面的代码。谢谢!

c# arrays sorting bubble-sort insertion-sort
3个回答
4
投票

这样做:

 Array.Sort(data);

3
投票

最有效的方法应该是使用quicksort的Array.Sort(data);

一些其他排序算法:

冒泡

public static void BubbleSort<T>(this List<T> source) where T : IComparable<T>
{
    IComparer<T> comparer = Comparer<T>.Default;
    for (int i = (source.Count - 1); i >= 0; i--)
    {
        for (int j = 1; j <= i; j++)
        {
            if (comparer.Compare(source[j - 1], source[j]) > 0)
            {
                var temp = source[j - 1];
                source[j - 1] = source[j];
                source[j] = temp;
            }
        }
    }
}

插入排序

public static void InsertionSort<T>(this List<T> source) where T : IComparable<T>
{
    IComparer<T> comparer = Comparer<T>.Default;
    int j;
    for (int i = 1; i < source.Count; i++)
    {
        var index = source[i];
        j = i;
        while ((j > 0) && comparer.Compare(source[j - 1], index) > 0)
        {
            source[j] = source[j - 1];
            j = j - 1;
        }
        source[j] = index;
    }
}

堆排序

public static void HeapSort<T>(this List<T> source) where T : IComparable<T>
{
    int heapSize = source.Count;
    for (int p = (heapSize - 1) / 2; p >= 0; p--)
    {
        HeapSort_sub(source, heapSize, p);
    }
    for (int i = source.Count - 1; i > 0; i--)
    {
        var temp = source[i];
        source[i] = source[0];
        source[0] = temp;
        heapSize--;
        HeapSort_sub(source, heapSize, 0);
    }
}

private static void HeapSort_sub<T>(List<T> source, int heapSize, int index)
{
    IComparer<T> comparer = Comparer<T>.Default;
    int left = (index + 1) * 2 - 1;
    int right = (index + 1) * 2;
    int largest = 0;
    if (left < heapSize && comparer.Compare(source[left], source[index]) > 0)
    {
        largest = left;
    }
    else
    {
        largest = index;
    }
    if (right < heapSize && comparer.Compare(source[right], source[largest]) > 0)
    {
        largest = right;
    }
    if (largest != index)
    {
        var temp = source[index];
        source[index] = source[largest];
        source[largest] = temp;
        HeapSort_sub(source, heapSize, largest);
    }
}

快速排序

public static void QuickSort<T>(this List<T> source) where T : IComparable<T>
{
    QuickSort_sub(source, 0, source.Count - 1);
}

private static void QuickSort_sub<T>(List<T> source, int left, int right)
{
    IComparer<T> comparer = Comparer<T>.Default;
    int i = left, j = right;
    var pivot = source[(left + right) / 2];
    while (i <= j)
    {
        while (comparer.Compare(source[i], pivot) < 0)
        {
            i++;
        }
        while (comparer.Compare(source[j], pivot) > 0)
        {
            j--;
        }
        if (i <= j)
        {
            var tmp = source[i];
            source[i] = source[j];
            source[j] = tmp;
            i++;
            j--;
        }
    }
    if (left < j)
    {
        QuickSort_sub(source, left, j);
    }
    if (i < right)
    {
        QuickSort_sub(source, i, right);
    }
}

臭皮匠排序

public static void StoogeSort<T>(this List<T> L) where T : IComparable
{
    StoogeSortSub(L, 0, L.Count - 1);
}

private static void StoogeSortSub<T>(List<T> L, int i, int j) where T : IComparable
{
    if (L[j].CompareTo(L[i]) < 0)
    {
        T tmp = L[i];
        L[i] = L[j];
        L[j] = tmp;
    }
    if (j - i > 1)
    {
        int t = (j - i + 1) / 3;
        StoogeSortSub(L, i, j - t);
        StoogeSortSub(L, i + t, j);
        StoogeSortSub(L, i, j - t);
    }
}

选择排序

public static void SelectionSort<T>(this List<T> source) where T : IComparable<T>
{
    IComparer<T> comparer = Comparer<T>.Default;
    int min;
    for (int i = 0; i < source.Count - 1; i++)
    {
        min = i;
        for (int j = i + 1; j < source.Count; j++)
        {
            if (comparer.Compare(source[j], source[min]) < 0)
            {
                min = j;
            }
        }
        var temp = source[i];
        source[i] = source[min];
        source[min] = temp;
    }
}

鸡尾酒排序

public static void CocktailSort<T>(this List<T> A) where T : IComparable<T>
{
    IComparer<T> comparer = Comparer<T>.Default;
    bool swapped;
    do
    {
        swapped = false;
        for (int i = 0; i <= A.Count - 2; i++)
        {
            if (comparer.Compare(A[i] , A[i + 1])> -1)
            {
                T temp = A[i];
                A[i] = A[i + 1];
                A[i + 1] = temp;
                swapped = true;
            }
        }
        if (!swapped)
        {
            break;
        }
        swapped = false;
        for (int i = A.Count - 2; i >= 0; i--)
        {
            if (comparer.Compare(A[i], A[i + 1]) > -1)
            {
                T temp = A[i];
                A[i] = A[i + 1];
                A[i + 1] = temp;
                swapped = true;
            }
        }   
    } while (swapped);
}

GnomeSort

public static void GnomeSort<T>(this List<T> a) where T : IComparable<T>
{
    IComparer<T> comparer = Comparer<T>.Default;
    int i = 1, j = 2;

    while (i < a.Count)
    {
        if (comparer.Compare(a[i - 1] ,a[i])<1)
        {
            i = j;
            j++;
        }
        else
        {
            T tmp = a[i - 1];
            a[i - 1] = a[i];
            a[i] = tmp;
            i -= 1;
            if (i == 0)
            {
                i = 1;
                j = 2;
            }
        }
    }
}

希尔排序

public static void ShellSort<T>(this List<T> source) where T : IComparable<T>
{
    IComparer<T> comparer = Comparer<T>.Default;
    int j, increment;
    increment = 3;
    while (increment > 0)
    {
        for (int i = 0; i < source.Count; i++)
        {
            j = i;
            var temp = source[i];
            while ((j >= increment) && comparer.Compare(source[j - increment], temp) > 0)
            {
                source[j] = source[j - increment];
                j = j - increment;
            }
            source[j] = temp;
        }
        if (increment / 2 != 0)
        {
            increment = increment / 2;
        }
        else if (increment == 1)
        {
            increment = 0;
        }
        else
        {
            increment = 1;
        }
    }
}

1
投票

.NET有一个默认的数组排序解决方案。 Array.Sort。默认情况下,.NET实现了一个Quicksort algorithm,它在O(n)和O(n log n)之间进行排序。 (这比bubble sort快得多)。

您可以通过执行Array.Sort(your_array)来使用它,或者如果您需要更复杂的东西(比如排序对象或反向排序),则会有一个带有IComparer对象的重载。

© www.soinside.com 2019 - 2024. All rights reserved.