public class Ex13LongestSortedSequence { public static int longestSortedSequence(int[] list) { int longest = 1; int currSoFar = 1; for (int i = 0; i < list.length-1; i++) { int n1 = list[i]; int n2 = list[i+1]; if (n1 <= n2) { currSoFar++; if (currSoFar > longest) { longest = currSoFar; } } else { // reinitialize currSoFar = 1; } } return longest; } public static void main(String[] args) { int[] list1 = { 3,8,10,1,9,14,-3,0,14,207,56,98,12 }; int[] list2 = { 3,8,10,1,9,14,-3,0,14,207,12,56,98,120,125,130 }; System.out.println( longestSortedSequence(list1) ); System.out.println( longestSortedSequence(list2) ); } }