簡體   English   中英

確定數組是否已排序

[英]Determining if an array is sorted

我有一個學校作業,用戶必須輸入數字,程序必須確定數字是否排序。 如果有人可以幫助我的代碼,那就太好了。 我在使用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;

    }
}

您沒有設置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

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

我希望這會在任務要求中為您提供大部分幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM