views:

91

answers:

2

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
+4  A: 

Test if ascending:

for item i in items
    if i > nextitem
       return false

return true
Stefan Kendall