Find whether an array is sorted or not.
class Prog10{
static boolean isSorted(int[] arr){
int flag = 0;
for(int i = 0 ; i<arr.length-1; i++){
if(arr[i] > arr[i+1]){
return false;
// Even if 1 item is greater then next item, ans is false.
}
}
return true;
// if all items are perfect only then code reaches till here, so return true.
}
public static void main(String[] args){
// args[0] -> no . of elements
int no = Integer.parseInt(args[0]);
int[] arr = new int[no];
for(int i = 0 ; i<no ; i++){
arr[i] = Integer.parseInt(args[i+1]);
}
if(Prog10.isSorted(arr)){
System.out.println("Yes sorted");
}
else{
System.out.println("Not sorted");
}
}
}
class Prog10{
static boolean isSorted(int[] arr){
int flag = 0;
for(int i = 0 ; i<arr.length-1; i++){
if(arr[i] > arr[i+1]){
return false;
// Even if 1 item is greater then next item, ans is false.
}
}
return true;
// if all items are perfect only then code reaches till here, so return true.
}
public static void main(String[] args){
// args[0] -> no . of elements
int no = Integer.parseInt(args[0]);
int[] arr = new int[no];
for(int i = 0 ; i<no ; i++){
arr[i] = Integer.parseInt(args[i+1]);
}
if(Prog10.isSorted(arr)){
System.out.println("Yes sorted");
}
else{
System.out.println("Not sorted");
}
}
}
No comments:
Post a Comment