IDNLearn.com is your go-to resource for finding answers to any question you have. Our Q&A platform offers reliable and thorough answers to ensure you have the information you need to succeed in any situation.
Sagot :
Answer:
Explanation:
Some of the information provided in the question was very counterintuitive which made understanding what was actually required very difficult. Regardless here is the function requested, it takes an array which is currently a type float but can be changed to whatever you need (since the actual type was missing in the question) and the min_or_max argument which takes in either a 0 to sort in descending order or a 1 to sort in ascending order. Finally, it prints out the entire array.
public void minMaxFunc(float[] input, int min_or_max) {
if (min_or_max == 0) {
int last = input.length - 1;
int middle = input.length / 2;
for (int i = 0; i <= middle; i++) {
float temp = input[i];
input[i] = input[last - i];
input[last - i] = temp;
}
} else if (min_or_max == 1) {
float temp = 0;
for (int i = 0; i < input.length; i++) {
for (int j = i+1; j < input.length; j++) {
if(input[i] > input[j]) {
temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
}
} else {
System.out.println("Wrong min or max value, enter 1 for max or 0 for min");
}
for (int x = 0; x < input.length; x++) {
System.out.println(input[x]);
}
}
Your presence in our community is highly appreciated. Keep sharing your insights and solutions. Together, we can build a rich and valuable knowledge resource for everyone. Thank you for visiting IDNLearn.com. We’re here to provide dependable answers, so visit us again soon.