+ 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!

16th Feb 2018, 9:51 PM
Nitzan
Nitzan - avatar
6 Answers
16th Feb 2018, 10:52 PM
Vukan
Vukan - avatar
16th Feb 2018, 10:04 PM
Obbu
Obbu - avatar
+ 1
all(x<=y for (x,y) in zip(l,l[1:]))
17th Feb 2018, 12:15 PM
VcC
VcC - avatar
+ 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; }
17th Feb 2018, 6:10 PM
Oguzhan Kelleci
Oguzhan Kelleci - avatar
+ 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; } }
17th Feb 2018, 7:27 PM
Oguzhan Kelleci
Oguzhan Kelleci - avatar