In: Computer Science
JAVA
Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the program first prompts the user to enter the size of the list. Using Array
My output should look like this
Enter the size of the list: 8
Enter the contents of the list: 10 1 5 16 61 9 11 1
The list has 8 integers 10 1 5 16 61 9 11 1
The list is not sorted
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
System.out.println("Enter the size
of the list : ");
int n = s.nextInt();
System.out.println("Enter the
contents in the list : ");
int a[] = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
int c1 = 0, c2 = 0;
for(int i = 0; i < n - 1; i
++)
{
if(a[i] > a[i + 1])
{
c1++;
}
if(a[i] < a[i + 1])
{
c2++;
}
}
if(c1 + 1 == n || c2 + 1 ==
n)
{
System.out.println("The list is
sorted");
}
else{
System.out.println("The list is not
sorted");
}
}
}