Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the code to determine whether an array is sorted. It should return \"tru

ID: 3619552 • Letter: T

Question

This is the code to determine whether an array is sorted. It should return "true" if an array is sorted. It should return "false" if the array is not sorted. What is wrong with my code?

public class St
{
   public static void main(String[] args)
   {
      int[] a = {1,2,4,5};
      System.out.println(falseOrTrue(a));

   }
         public static boolean falseOrTrue(int[] array)
         {
               for(int i = 0 ; i< array.length-1 ; i++)
               {
                     if(array[i]>array[i+1])
                     {
                           return true;
                     }
                     else
                     {
                           return false;
                     }

               }

          }
}

Explanation / Answer

please rate - thanks public class St
{
   public static void main(String[] args)
   {
      int[] a = {1,2,4,5};
      System.out.println(falseOrTrue(a));

   }
         public static boolean falseOrTrue(int[] array)
         {
               for(int i = 0 ; i< array.length-1 ; i++)
               {
                     if(array[i]>array[i+1])
                     {
                           return false;         //as soon as 1 element is in wrong spot return false
                     }
                }
               return true;                  //return true only if the entire array is in the correct order
              }
}
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote