Question
java code
Write a method that given a list of integers as a reference to its first node, determines if the list is sorted in non-decrea
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Well,according to the question program has to determine whether the list of integers is sorted in non-decreasing order i.e in increasing order or ascending order.

Below is the source code along with the comments to understand it better.

Source Code:-

import java.util.Scanner;//to use scanner function
public class Main  //class named main
{
        public static void main(String[] args) //main function
        {
            Scanner sc = new Scanner(System.in);
            int n,count=0;
            System.out.println("Enter the number of integers ");
            n=sc.nextInt();
            int arr[] = new int[n];
            System.out.println("Enter the integers:-");
            for(int i=0;i<n;i++)
            {
                arr[i]=sc.nextInt();//taking integers from the user
            }
            for(int i=0;i<n-1;i++)
            {
                if(arr[i]<arr[i+1])
                count++;               //determining whether it is sorted or not
            }
            if(count==n-1)
            System.out.println("Yes,it is sorted in non descending order");
            else
            System.out.println("No,it is not sorted in non descending order");//finally printing results
        }
}

Screenshot of the source code(Refer for better understanding of identation):-

import java.util.Scanner;//to use scanner function public class Main //class named main { public static void main(String[] ar

Output of the code:-

Enter the number of integers 5 Enter the integers:- 2 5 7 9 45 Yes, it is sorted in non descending orderEnter the number of integers 6 Enter the integers:- 6 5 4 3 2 1 No, it is not sorted in non descending order |

Hope it helps...

Thankyou...... :-)

Add a comment
Know the answer?
Add Answer to:
java code Write a method that given a list of integers as a reference to its...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT