Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a static method named isSorted that accepts an array of doubles as a param

ID: 662703 • Letter: W

Question

Write a static method named isSorted that accepts an array of doubles as a parameter and returns true if the list is in sorted (nondecreasing) order and false otherwise. For example, if arrays named list1 and list2 store {16.1, 12.3, 22.2, 14.4} and {1.5, 4.3, 7.0, 19.5, 25.1, 46.2} respectively, the calls isSorted(list1) and isSorted(list2) should return false and true respectively. Assume the array has at least one element. A one-element array is considered to be sorted.

Java language only and please make sure it works.

Explanation / Answer

import java.util.Arrays;
import java.lang.*;
public class Sort_Test{

public static void main(String []args){
// System.out.println("Hello World");
double iArr[] = {16.1, 12.3, 22.2, 14.4};
  
double[] cArr = new double[iArr.length];
for(int i=0;i<iArr.length;i++){
cArr[i]=iArr[i];
}
// Arrays.sort(iArr);
// int count = countInRange(iArr,cArr);
System.out.println(isSorted(iArr,cArr));
  
}
public static boolean isSorted(double[] iArr,double[] cArr){
Arrays.sort(iArr);
boolean flag=true;
for(int i=0;i<iArr.length;i++){
if(iArr[i] != cArr[i]){flag=false;}
}
return flag;
}
}

output: false //{16.1, 12.3, 22.2, 14.4} Not sorted

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote