C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: We loop over arrays, comparing adjacent elements for the correct ordering. This is a faster approach.
ForMain: This declares 6 arrays, 3 for ints and 3 for strings. Each trio contains one ascending array, one unsorted array, and one descending array.
ArraySortTools: The SortTools class is a public static class that you can put in an external file, called "SortTools.cs".
StaticIsSorted int: IsSorted checks that the int array specified is sorted in ascending order.
IsSorted string: This method works the same as its int equivalent. It calls CompareTo on strings.
C# program that tests sorted arrays
using System;
class Program
{
static void Main()
{
int[] sortedInts = new int[]
{
1,
4,
6,
8
};
int[] unsortedInts = new int[]
{
5,
2,
7,
6
};
int[] reversedInts = new int[]
{
9,
8,
5,
1
};
string[] sortedStrings = new string[]
{
"cat",
"man",
"zebra"
};
string[] unsortedStrings = new string[]
{
"soda",
"pop",
"coke",
"tonic"
};
string[] reversedStrings = new string[]
{
"zebra",
"partridge",
"apple"
};
Console.WriteLine(SortTools.IsSorted(sortedInts)); // True
Console.WriteLine(SortTools.IsSortedDescending(sortedInts)); // False
Console.WriteLine(SortTools.IsSorted(unsortedInts)); // False
Console.WriteLine(SortTools.IsSortedDescending(unsortedInts)); // False
Console.WriteLine(SortTools.IsSorted(reversedInts)); // False
Console.WriteLine(SortTools.IsSortedDescending(reversedInts)); // True
Console.WriteLine(SortTools.IsSorted(sortedStrings)); // True
Console.WriteLine(SortTools.IsSortedDescending(sortedStrings)); // False
Console.WriteLine(SortTools.IsSorted(unsortedStrings)); // False
Console.WriteLine(SortTools.IsSortedDescending(unsortedStrings)); // False
Console.WriteLine(SortTools.IsSorted(reversedStrings)); // False
Console.WriteLine(SortTools.IsSortedDescending(reversedStrings)); // True
}
}
/// <summary>
/// Methods for determining if arrays are sorted.
/// </summary>
public static class SortTools
{
/// <summary>
/// Determines if int array is sorted from 0 -> Max
/// </summary>
public static bool IsSorted(int[] arr)
{
for (int i = 1; i < arr.Length; i++)
{
if (arr[i - 1] > arr[i])
{
return false;
}
}
return true;
}
/// <summary>
/// Determines if string array is sorted from A -> Z
/// </summary>
public static bool IsSorted(string[] arr)
{
for (int i = 1; i < arr.Length; i++)
{
if (arr[i - 1].CompareTo(arr[i]) > 0) // If previous is bigger, return false
{
return false;
}
}
return true;
}
/// <summary>
/// Determines if int array is sorted from Max -> 0
/// </summary>
public static bool IsSortedDescending(int[] arr)
{
for (int i = arr.Length - 2; i >= 0; i--)
{
if (arr[i] < arr[i + 1])
{
return false;
}
}
return true;
}
/// <summary>
/// Determines if string array is sorted from Z -> A
/// </summary>
public static bool IsSortedDescending(string[] arr)
{
for (int i = arr.Length - 2; i >= 0; i--)
{
if (arr[i].CompareTo(arr[i + 1]) < 0) // If previous is smaller, return false
{
return false;
}
}
return true;
}
}
Output
True
False
False
False
False
True
True
False
False
False
False
True