Write a method called isSorted that takes an array of doubles as a parameter and
ID: 3554944 • Letter: W
Question
Write a method called isSorted that takes an array of doubles as a parameter and returns true if the list is in sorted (non-decreasing) order and returns 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.
Please show your answer in a complete java program only please. Thank you!
Explanation / Answer
private static void isSorted(double[] array) { List boxed = new ArrayList(); for (int i : array) { boxed.add(i); } List ascending = new ArrayList(boxed); Collections.sort(ascending); List descending = new ArrayList(ascending); Collections.reverse(descending); if (boxed.equals(ascending)) { System.out.println("Ascending"); } else if (boxed.equals(descending)) { System.out.println("Descending"); } else { System.out.println("Unsorted"); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.