Home > Software engineering >  Check if current loop iteration is greater than the previous one
Check if current loop iteration is greater than the previous one

Time:09-17

I have to check if a int array is sorted in ascended order so like 1,2,3,4 etc..

So this is my attempt in pseudo code:

int[] arrayName = {1,2,3,4,5};   // This should be true
int[] arrayName2 = {5,4,3,6};    // This should be false

for (int i = 0; i < arrayName.Length; i  ) 
{
    if (arrayName[i] < arrayName[i] - 1)
    {
        Console.WriteLine("The array is sorted");
    }
    else 
        Console.WriteLine("The array is not sorted");
}

My question: is their a way to check the current iteration against the previous iteration also I cannot use any library or extension for this exercises so basically I can only use "System"

For example:

if (currentIteration > previousIteration) 
    Console.WriteLine("The array is sorted");
else 
    Console.WriteLine("The array is not sorted");

CodePudding user response:

An array is sorted only if x[i] <= x[i 1] for all possible values of i

So let's start off assuming it's sorted. If we find a single pair of values that don't satisfy our condition, the array isn't sorted.

bool isSorted = true;
for (int i = 0; i < arrayName.Length - 1; i  ) {
    if (arrayName[i] > arrayName[i   1]) {
        isSorted = false; 
        break; // One pair isn't sorted, so we don't need to check anything else
    }
}

Now that you've checked everything you need, you can output.

if (isSorted)
    Console.WriteLine("Array is sorted");
else
    Console.WriteLine("Not sorted");

CodePudding user response:

Start iterating at index 1, then you can refer to the previous item with arrayName[i - 1]. Note that the -1 must be applied to the index, not to the array value arrayName[i] - 1.

Also, you want to print the outcome after you have tested the array, not at each iteration. It's best to create a function, so that you can apply it to several arrays easily.

static bool IsArraySorted(int[] a)
{
    for (int i = 1; i < a.Length; i  ) {
        if (a[i] < a[i - 1]) {
            return false; // We don't need to test the rest of the array.
        }
    }
    return true;
}

Now, you can use it like this

if (IsArraySorted(arrayName)) {
    Console.WriteLine("The array is sorted");
} else {
    Console.WriteLine("The array is not sorted");
}

CodePudding user response:

Ooh a homework question. Might as well have some fun. This can be done with Enumerable generically

bool isSorted<T>(IEnumerable<T> a) where T : IComparable => Enumerable.Range(1, a.Count() - 1).All(i => a.ElementAt(i).CompareTo(a.ElementAt(i - 1)) >= 0);

Call it

int[] arrayName = { 1, 2, 3, 4, 5 }; //This one should be true
int[] arrayName2 = { 5, 4, 3, 6 }; //This one should be false
double[] arrayName3 = { 4, 4, 8, 11.5 }; //This one should be true
var arrayName4 = new Single[] { 3F, 4.5F, 6F, 1.0F }.ToList(); //This one should be false
var arrayName5 = new string[] { "a", "abb", "b", "c" }; //True
var arrayName6 = new string[] { "a", "a", "b", "a" }; //False

Console.WriteLine(isSorted(arrayName));
Console.WriteLine(isSorted(arrayName2));
Console.WriteLine(isSorted(arrayName3));
Console.WriteLine(isSorted(arrayName4));
Console.WriteLine(isSorted(arrayName5));
Console.WriteLine(isSorted(arrayName6));

True
False
True
False
True
False

Generic and reusable.

Also

bool isSorted2<T>(IEnumerable<T> a) where T : IComparable => a.SequenceEqual(a.OrderBy(b => b));

which involves sorting the array in memory but looks neater.

  • Related