The file below is the starter file for the question: public class Sorted { publi
ID: 3813825 • Letter: T
Question
The file below is the starter file for the question:
public class Sorted {
public static void main(String[] args) {
int[] array = new int[6];
for (int i = 0; i < array.length; i++) {
array[i] = i;
System.out.printf("%d ", array[i]);
}
System.out.println("");
printIsSorted(isSorted(array));
for (int i = 0; i < array.length; i++) {
array[i] = (int)(Math.random() * 20);
System.out.printf("%d ", array[i]);
}
System.out.println("");
printIsSorted(isSorted(array));
}
public static void printIsSorted(boolean sorted) {
// Your code here
}
}
Provide code for the method
public static void printIsSorted(boolean sorted)
that prints “The array is sorted” if the given boolean value sorted is true and prints “The array is not sorted” otherwise.
Please also provide comments for each line of the program.
must also be written in java C++
Explanation / Answer
HI, Please find my implementation.
public class Sorted1 {
public static void main(String[] args) {
int[] array = new int[6];
for (int i = 0; i < array.length; i++) {
array[i] = i;
System.out.printf("%d ", array[i]);
}
System.out.println("");
printIsSorted(isSorted(array));
for (int i = 0; i < array.length; i++) {
array[i] = (int)(Math.random() * 20);
System.out.printf("%d ", array[i]);
}
System.out.println("");
printIsSorted(isSorted(array));
}
public static boolean isSorted(int[] array) {
// Your code here
if(array == null || array.length <=1)
return true;
for(int i=0; i<array.length-1; i++)
if(array[i] > array[i+1])
return false;
return true;
}
public static void printIsSorted(boolean sorted) {
// Your code here
if(sorted)
System.out.println("The array is sorted");
else
System.out.println("The array is not sorted");
}
}
/*
Sample run:
0 1 2 3 4 5
The array is sorted
2 19 5 4 0 11
The array is not sorted
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.