In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how Java finds the maximum value through recursive comparison, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.
recursive contrast
The core of recursive comparison is to define two positions (start position and end position), compare the size of the start position and end position values each time, when the value of the start position is greater than the end position value, set the maximum value to the value of the start position, and then move the end position-1 (one bit forward), continue to recursively call; On the contrary, when the value of the end position is greater than the start position, set the maximum value to the value of the end position, move the start position +1 (one bit later), and continue to recursively call the comparison until the recursion ends. The maximum value can be returned. The execution process is as shown in the following figure:
The implementation code is as follows:
public class ArrayMax {
public static void main(String[] args) {
int[] arr = {3, 7, 2, 1, -4};
int max = findMaxByRecursive(arr, 0, arr.length - 1, 0); //Find the maximum value according to Collections
System.out.println("max is: " + max);
}
/**
* Maximum value according to recursive query
* @param arr Array to be queried
* @param head subscript of the first element
* @param last subscript of the last element
* @param max (temporary) maximum
* @return Maximum
*/
private static int findMaxByRecursive(int[] arr, int head, int last, int max) {
if (head == last) {
//recursion completed, return result
return max;
} else {
if (arr[head] > arr[last]) {
max = arr[head]; //assign maximum value
//move recursively from back to front
return findMaxByRecursive(arr, head, last - 1, max);
} else {
max = arr[last]; //assign maximum value
//move recursively from front to back
return findMaxByRecursive(arr, head + 1, last, max);
}
}
}
}
The results of the above procedures are:
Maximum is: 7
Thank you for reading this article carefully. I hope that the article "How Java finds the maximum value through recursive comparison" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.