Add method for checking if a given array is in ascending order.

This commit is contained in:
Kristian Stolen
2022-01-12 19:00:20 +08:00
parent 8e54c0e930
commit b1cfa83ac5
2 changed files with 31 additions and 0 deletions
@@ -48,5 +48,23 @@ namespace Reverse.Tests
Assert.True(input.SequenceEqual(output));
}
[Theory]
[InlineData(new int[] { 1 })]
[InlineData(new int[] { 1, 2 })]
[InlineData(new int[] { 1, 1 })]
public void IsArrayInAscendingOrder_WhenArrayElementsAreInNumericAscendingOrder_ReturnsTrue(int[] input)
{
var result = Reverser.IsArrayInAscendingOrder(input);
Assert.True(result);
}
[Fact]
public void IsArrayInOrder_WhenArrayElementsAreNotInNumericAscendingOrder_ReturnsFalse()
{
var result = Reverser.IsArrayInAscendingOrder(new int[] { 2, 1 });
Assert.False(result);
}
}
}
@@ -17,5 +17,18 @@
arrayToReverse[upperIndex] = temp;
}
}
public static bool IsArrayInAscendingOrder(int[] array)
{
for (int i = 1; i < array.Length; i++)
{
if (array[i] < array[i - 1])
{
return false;
}
}
return true;
}
}
}