I need an Algorithm which is used to find the N-element randomly-ordered integer array is either already sorted or not.
+7
A:
Just loop through the array til you find an element that is less than the previous one. In C/Java'ish pseudo-code:
int prev = array[0];
boolean sorted = true;
for (int i=1; i<array.length; i++) {
if (array[i] < prev) {
sorted = false;
break;
}
prev = array[i];
}
cletus
2009-12-02 04:43:02
+4
A:
Test if ascending:
for item i in items
if i > nextitem
return false
return true
Stefan Kendall
2009-12-02 04:43:38