import java.util.*; import java.lang.*; public class Example6 { public static void main (String [] args) { int numbers[] = {1145,22242,313422,4124242,51234242,42222222}; boolean sorted = true; int k = 0; int size = numbers.length - 1; while (sorted == true && k < size) { int A1 = numbers[k]; int AN = numbers [k+1]; System.out.print (A1); System.out.println (AN); if (A1 < AN) { k++; } else { sorted = false; } } if(sorted) System.out.println("Sorted"); else System.out.println ("Not Sorted"); } }
This program will work for any set of numbers because it will check to see if the first number is less than the second number and so on and so forth until the end of the array. It will only go through the array one time so it will finish in a finite time.
No comments:
Post a Comment