The file below is the starter file for the question: public class Sorted { publi
ID: 3813823 • 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 boolean isSorted(int[] array) {
// Your code here
}
Provide code for the method
public static boolean isSorted(int[] array)
that returns true if the given array is sorted and returns false 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.
Please let me know in case of any issue.
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("");
System.out.println(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("");
System.out.println(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;
}
}
/*
Sample run:
0 1 2 3 4 5
true
11 3 11 12 9 19
false
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.