Tuesday, 15 February 2011

algorithm - Check the sortedness Of An Array -


I need an algorithm that is used to find the N-element randomly sorted integer array Already sorted or not. / P>

Just loop through the array, you can find the element that is less than the previous one. Phony Code in C / Java:

  int prev = array [0]; Boolean sorted = true; For (int i = 1; i & lt; array.label; i ++) {if (array [i] & lt; prev) {sorted = false; break; } Prev = array [i]; }  

No comments:

Post a Comment