在列表框 + IMG 中快速排序随机数



我正在尝试对列表框中的 10 个随机数进行快速排序。但是我不能在我的随机 iar 上使用该方法,任何人都可以给我一些建议。

代码隐藏按钮:

         private void btnSort_Click(object sender, EventArgs e)
    {
        Random r = new Random();
        int n = 10;
        int[] iar = new int[n];
        for (int i = 0; i < iar.Length; i++)
        {
            iar[i] = r.Next(0, 20);
            lb1.Items.Add(iar[i]);
        //here is the error i want to fill lb2 with the quicksorted array 
        // using the quicksort method
            Quicksort(iar, 0, iar.Length - 1);
        }
        for (int i = 0; i < iar.Length; i++)
        {
            lb2.Items.Add(iar[i]);
        }
    }

快速排序方法

 public static void Quicksort(IComparable[] elements, int left, int right)
    {
        int i = left, j = right;
        IComparable pivot = elements[(left + right) / 2];
        while (i <= j)
        {
            while (elements[i].CompareTo(pivot) < 0)
            {
                i++;
            }
            while (elements[j].CompareTo(pivot) > 0)
            {
                j--;
            } 
            if (i <= j)
            {
                // Swap
                IComparable tmp = elements[i];
                elements[i] = elements[j];
                elements[j] = tmp;
                i++;
                j--;
            }
        }
        // Recursive calls
        if (left < j)
        {
            Quicksort(elements, left, j);
        }
        if (i < right)
        {
            Quicksort(elements, i, right);
        }
    }
}

错误:

错误 2 参数 1:无法从"int[]"转换为"System.IComparable[]"
错误 1 "Quicksort.FrmQuicksort.Quicksort(System.IComparable[], int, int)"的最佳重载方法匹配有一些无效参数

谢谢你的寻找:)

在给定的上下文中,实际上不需要指定IComparable而不是int,因为您只使用整数。您还会调用QuickSort因为每个数字都是生成的,如果您在已生成的列表中运行 QuickSort 算法,因此请将语句移出循环,则每个数字的效果最好。还要记住,有更好的排序算法来对像这样的微量数据进行排序。

单击事件处理程序:

private void btnSort_Click(object sender, EventArgs e)
{
    Random r = new Random();
    int n = 10;
    int[] iar = new int[n];
    //Generate random numbers and store them in the Unsorted ListBox
    for (int i = 0; i < iar.Length; i++)
    {
        iar[i] = r.Next(0, 20);
        lb1.Items.Add(iar[i]);
    }
    //Sort the random numbers array
    Quicksort(iar, 0, iar.Length - 1);
    //Now the array is sorted put it in the Sorted Listbox
    for (int i = 0; i < iar.Length; i++)
    {
        lb2.Items.Add(iar[i]);
    }
}

快速排序功能

//Take an array of integers not an array of IComparable
public static void Quicksort(int[] elements, int left, int right)
{
    int i = left;
    int j = right;
    int pivot = elements[(left + right) / 2];
    while (i <= j)
    {
        while (elements[i].CompareTo(pivot) < 0)
        {
            i++;
        }
        while (elements[j].CompareTo(pivot) > 0)
        {
            j--;
        }

        if (i <= j)
        {
            // Swap
            int tmp = elements[i];
            elements[i] = elements[j];
            elements[j] = tmp;
            i++;
            j--;
        }
    }
    // Recursive calls
    if (left < j)
    {
        Quicksort(elements, left, j);
    }
    if (i < right)
    {
        Quicksort(elements, i, right);
    }
}

由于您已经声明了Quicksort方法以获取IComparable对象的数组,因此您需要向它传递一个IComparable对象的数组,而不是int数组。一个明显的解决方案是声明一个包装类:

    class MyQuicksortableInt : IComparable
    {
        public int Value { get; set; }
        // Implementation of IComparable left as an exercise for the reader;
        // see http://msdn.microsoft.com/en-us/library/System.IComparable.aspx for details
    }

iar更改为 MyQuicksortableInt[] 类型,您现有的Quicksort代码将愉快地接受它。

你需要这样做:

public static void Quicksort<T>(T[] elements, int left, int right) where T:IComparable<T>

而不是

public static void Quicksort(IComparable[] elements, int left, int right)

并在之后到处使用 T 而不是 IComparable

最新更新