-1

前提条件:有一个 1000 个整数的有序数组(来自文本文件)。

后置条件:使用递归选择排序对整数数组进行排序。

该代码根本不显示任何结果。我添加了递归方法,在主方法中我调用了 RecursiveSelection 方法。

我尝试将 Recursive 方法设为 int 和 int[] 方法,但它仍然没有给出任何结果。

public static void RecursiveSelection(int[] Array)
{
    Console.WriteLine("Array after Recursive Selection Sort: ");

    SelectionSortRecursive(Array, 0); // initial recursive call            
}

private static void SelectionSortRecursive(int[] Array, int n) // sorted in ascending order recursively
{
    if (n >= Array.Length - 1)
        return;
    int min = n;
    for (int i = n+1; i < Array.Length; i++)
    {
        if (Array[i] < Array[min])
            min = i;
    }
    swap(Array, n, min);
    SelectionSortRecursive(Array, n + 1);     
}

public static void swap(int[] Array,int x, int y)
{
    int temp = Array[x];
    Array[x] = Array[y];
    Array[y] = temp;
}

输出是Console.Writeline语句“递归选择排序后的数组...”,但没有显示任何其他内容。我希望它显示排序列表。

4

1 回答 1

1
class Program
{
    static void Main(string[] args)
    {
        int[] array = { 344, 334, 200, 188, 39, 981, 33 };

        Console.WriteLine("Array after Recursive Selection Sort: ");

        SelectionSortRecursive(array, 0); // initial recursive call

        foreach (int i in array)
        {
            Console.WriteLine(i);
        }

        Console.ReadKey();
    }

    private static void SelectionSortRecursive(int[] Array, int n) // sorted in ascending order recursively
    {
        if (n >= Array.Length - 1)
            return;
        int min = n;
        for (int i = n + 1; i < Array.Length; i++)
        {
            if (Array[i] < Array[min])
                min = i;
        }
        swap(Array, n, min);
        SelectionSortRecursive(Array, n + 1);
    }

    public static void swap(int[] Array, int x, int y)
    {
        int temp = Array[x];
        Array[x] = Array[y];
        Array[y] = temp;
    }
}

输出

于 2019-04-22T12:20:53.203 回答