确定数组是否已排序

时间:2014-03-17 04:04:54

标签: c# arrays sorting int

我有一个学校作业,用户必须输入数字,程序必须确定它们是否已分类。如果有人可以帮助我的代码,这将是非常棒的。我在使用IsSorted(int[] array, int n)时遇到问题,truefalse无效。

以下是问题: Q7:编写一个程序来输入一个int数组,然后确定该数组是否已排序。该 程序应该有两个用户定义的方法来帮助您完成任务。

public static void InputArray(int[] array, ref int n)
public static bool IsSorted(int[] array, int n)

InputArray()应该类似于实验4中的填充。IsSorted()应该只返回true就是 数组按升序排序,否则为false。请注意,你不是 要求对数组进行排序,只需确定数组是否已排序。 Main方法应该给出 用户可以选择检查多个数组(即循环)。你可以假设 最大值数为20.

* * 注意:在此分配中,您可以采用正确的数据类型:即,如果程序 请求double,您可以假设用户将输入double等。您需要验证 输入的数据在正确的范围内。

到目前为止,这是我的代码:

using System;
public class Array_Sort
{
    public static void Main()
    {
        int n = 0;
        const int SIZE = 20;
        int[] array = new int[SIZE];

        InputArray(array, ref n);
        IsSorted(array, n);
    }

    public static void InputArray(int[] array, ref int SIZE)
    {
        int i = 0;


        Console.Write("Enter the number of elements: ");
        SIZE = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter the {0} elements:", SIZE);
        for (i = 0; i < SIZE; ++i)
            array[i] = Convert.ToInt32(Console.ReadLine());



    }

    public static bool IsSorted(int[] array, int n)
    {
        int last = 0;

        foreach (int val in array)
        {
            if (val < last)
                return false;
        }

        return true;

    }
}

2 个答案:

答案 0 :(得分:1)

您没有设置last变量。

public static bool IsSorted(int[] array, int n)
{
    int last = array[0];

    foreach (int val in array)
    {
        if (array[val] < last)
            return false;

        last = array[val+1];
    }

    return true;
}

这应该假设第一次检查始终有效。即array[0] >= 0

答案 1 :(得分:0)

public class Array_Sort
{
    public static int n = 0;
    public static int SIZE = 20;
    public static int[] array = new int[SIZE];

    public static void Main()
    {
        InputArray();
        if (IsSorted())
        {
            Console.WriteLine("\n  The values in the array are in Ascending order");
        }
        else
        {
            Console.WriteLine("\n  The values in the array are NOT in Ascending order");
        }
    }

    public static void InputArray()
    {
        int i = 0;
        Console.Write("\n Enter the number of elements  : ");
        SIZE = Convert.ToInt32(Console.ReadLine());
        if (SIZE > 20)
        {
            Console.WriteLine("\n Invalid Selection, try again \n\n ");
            InputArray();
        }
        else
        {
            for (i = 0; i < SIZE; ++i)
            {
                Console.WriteLine("\n Enter the element- {0}  : ", i + 1);
                array[i] = Convert.ToInt32(Console.ReadLine());
            }
        }
    }
    public static bool IsSorted()
    {
        int i;
        int count = 1;
        for (i = 0; i < n; i++)
        {
            if (i >= 1)
            {
                if (array[i] > array[i - 1])
                {
                    count++;
                }
            }
        }

        if (count == n)
            return true;
        else
            return false;
    }
}

我希望这可以帮助你完成作业所要求的大部分内容。

相关问题