如何调用我的并行快速排序函数

本文关键字:我的 并行 快速排序 函数 调用 何调用 | 更新日期: 2023-09-27 18:35:21

尝试从Main调用以下QuicksortParallel方法时出现编译错误。我不清楚要传递哪些参数来消除错误。我的目标是对数组进行排序并最终打印出来。

public static void QuicksortParallel<T>(List<T> arr, int left, int right)
    where T : IComparable<T>
{
    const int Threshold = 2048;
    if (right <= left)
        return;
    if (right - left < Threshold)
        Quicksort(arr, left, right);
    else
    {
        var pivot = Partition(arr, left, right);
        // Sorting the left and right of the pivot in parallel.
        Parallel.Invoke(
            () => QuicksortParallel(arr, left, pivot - 1),
            () => QuicksortParallel(arr, pivot + 1, right)
            );
    }
}
static void Main(string[] args)
{
    string[] unsorted = { "z", "e", "x", "c", "m", "q", "a" };
    // Print the unsorted array.
    for (int i = 0; i < unsorted.Length; i++)
        Console.Write(unsorted[i] + " ");
    Console.WriteLine();
    // Sort the array.
    QuicksortParallel(unsorted, 0, unsorted.Length - 1); // <-- Error here.
    // Print the sorted array.
    for (int i = 0; i < unsorted.Length; i++)
        Console.Write(unsorted[i] + " ");
    Console.WriteLine();
    Console.ReadKey();
}

那么,我应该给函数QuicksortParallel什么Main来对数组进行排序呢?

如何调用我的并行快速排序函数

static void Main(string[] args)
{
    string[] unsorted = { "z", "e", "x", "c", "m", "q", "a" };
    // Print the unsorted array.
    for (int i = 0; i < unsorted.Length; i++)
        Console.Write(unsorted[i] + " ");
    Console.WriteLine();
    // Get a copy of the array, as a list.
    List<string> list = unsorted.ToList();
    // Sort the list.
    QuicksortParallel(list, 0, list.Count - 1);
    // Print the sorted list.
    for (int i = 0; i < list.Count; i++)
        Console.Write(list[i] + " ");
    Console.WriteLine();
    Console.ReadKey();
}