【问题标题】:I want an efficient sorting algorithm to sort an array我想要一个有效的排序算法来对数组进行排序
【发布时间】:2016-06-09 19:15:36
【问题描述】:
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;
    }
}

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

【问题讨论】:

  • 它是冒泡排序,因为最大数量像冒泡一样上升。
  • 你的目标不是很清楚。如果您只需要对数组进行排序 - 那么Array.Sort 方法就足够了。如果您出于某些教育原因需要实施插入排序 - 好吧,尝试实施它并在遇到具体问题时回来。
  • 您想知道这个 ocde 是哪种算法,还是需要更高效(无论这意味着什么)的算法?
  • 在这个例子中什么是数据......正如我所期望看到的 array.length
  • 如果您想要高效排序,请使用 .Net 框架提供的排序。如果你的目标实现插入排序(它不在高效排序列表的顶部),那么学习算法并实现它。

标签: c# arrays sorting bubble-sort insertion-sort


【解决方案1】:

最有效的方法应该是使用快速排序的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);
    }
}

StoogeSort

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;
            }
        }
    }
}

ShellSort

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;
        }
    }
}

【讨论】:

    【解决方案2】:

    这样做:

     Array.Sort(data);
    

    【讨论】:

    • 您为什么使用.ToList()List&lt;T&gt; 实现 .Sort()
    • 好吧,Eshqna 提到它是一个数组,没有一个列表
    • 为什么要打扰List?为什么不直接Array.Sort
    • 数组没有排序方法,列表有
    • @AshkanMobayenKhiabani SortArray 类的静态方法。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2017-05-21
      相关资源
      最近更新 更多