+ 5
Challenge is the array sorted
Write code that check if an array is sorted. For example: Input : {1,2,3,7,13,21} Output : True Input : {8,12,26,15,89,300} Output : False Have fun and keep it simple!
6 Answers
+ 7
+ 1
all(x<=y for (x,y) in zip(l,l[1:]))
+ 1
public static bool listordened(int[] listt)
{
for (int i = listt.Length - 1; i >= 1; i--)
{
for (int j = i- 1; j >= 0; j--)
{
if (listt[i] < listt[j])
{
return false;
}
}
}
return true;
}
+ 1
If you want a faster one (O(n))
public static bool listordened(int[] listt)
{
for (int i = listt.Length - 1; i >= 1; i--)
{
if (listt[i] < listt[i-1])
{
return false;
}
}
return true;
}
}