当前位置:网站首页>Recursive way
Recursive way
2022-07-06 18:12:00 【eight hundred and forty-eight million six hundred and ninety-ei】
recursive
Recursive method to achieve the maximum value of the array
@Test
public void TestMax() {
int a[] = {
0, 1, 2, 5, 3, 2, 1, 8, 4, 2, 0};
System.out.println(getMax(a));
}
public int getMax(int[] arr) {
return recursion(arr, 0, arr.length - 1);
}
//arr[l...r] Find the maximum value on the range
public int recursion(int[] arr, int l, int r) {
if (l == r) {
//arr[l...r] There is only one number in the range , Go straight back to
return arr[l];
}
int mid = l + ((r - l) >> 1); // Finding the midpoint , Prevent overflow
int leftMax = recursion(arr, l, mid);
int rightMax = recursion(arr, mid + 1, r);
return Math.max(leftMax, rightMax);
}
Merge sort
- The core idea used
Divide and conquer
Local sort first ,,,,,, Then sort the whole
First, divide a complete array into two halves ( Left half , Right half , Then divide the left side into two halves ,,,,, Until it can't be further divided , Then sort ,,,, Left row , The right row ...) Finally, the overall order
Space for time
Time complexity O(nlogn)
@Test
public void sort(){
int a[]={
2,5,4,6,9,7,1,0};
process(a,5,7);
for (int i = 0; i < 8; i++) {
System.out.print(a[i]);
}
}
/** * recursive , Bisection order * @param arr * @param l * @param r */
public void process(int[] arr, int l, int r) {
if (l == r) {
return;
}
int mid = l + ((r - l) >> 1);
process(arr, l, mid); // Order on the left
process(arr, mid + 1, r); // Order on the right
merge(arr, l, mid, r);
}
/*** * Sort * @param arr * @param l * @param mid * @param r */
private void merge(int[] arr, int l, int mid, int r) {
int[] help = new int[r - l + 1]; // Store the locally ordered array
int i = 0; // Index for new array
int j = l;
int k = mid + 1;
while (j <= mid && k <= r) {
help[i++] = arr[j] <= arr[k] ? arr[j++] : arr[k++];
}
while (j<=mid){
help[i++] = arr[j++];
}
while (k<=r){
help[i++] = arr[k++];
}
/** * Assign the ordered new array to the old array */
for (i = 0; i < help.length; i++) {
arr[l+i] = help[i];
}
}
Extension of merge sort
边栏推荐
- 【Swoole系列2.1】先把Swoole跑起来
- How to output special symbols in shell
- The shell generates JSON arrays and inserts them into the database
- Mysqlimport imports data files into the database
- Jerry's watch reads the file through the file name [chapter]
- STM32 key state machine 2 - state simplification and long press function addition
- MySQL 8 sub database and table backup database shell script
- 开源与安全的“冰与火之歌”
- Shell input a string of numbers to determine whether it is a mobile phone number
- 模板于泛型编程之declval
猜你喜欢
There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
Declval (example of return value of guidance function)
Interview shock 62: what are the precautions for group by?
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
虚拟机VirtualBox和Vagrant安装
2019阿里集群数据集使用总结
Open source and safe "song of ice and fire"
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
Jerry's updated equipment resource document [chapter]
随机推荐
Alibaba brand data bank: introduction to the most complete data bank
Appium automated test scroll and drag_ and_ Drop slides according to element position
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
趣-关于undefined的问题
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
Distinguish between basic disk and dynamic disk RAID disk redundant array
The easycvr authorization expiration page cannot be logged in. How to solve it?
Jerry is the custom background specified by the currently used dial enable [chapter]
2019阿里集群数据集使用总结
李書福為何要親自掛帥造手機?
Transfer data to event object in wechat applet
第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
Prophet模型的简介以及案例分析
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
STM32按键状态机2——状态简化与增加长按功能
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
模板于泛型编程之declval
VR panoramic wedding helps couples record romantic and beautiful scenes
Will openeuler last long