当前位置:网站首页>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

边栏推荐
- Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
- Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- 2022暑期项目实训(二)
- Will openeuler last long
- 二分(整数二分、实数二分)
- MS-TCT:Inria&SBU提出用于动作检测的多尺度时间Transformer,效果SOTA!已开源!(CVPR2022)...
- Compilation principle - top-down analysis and recursive descent analysis construction (notes)
- Interesting - questions about undefined
- Jielizhi obtains the currently used dial information [chapter]
- 第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
猜你喜欢

ASEMI整流桥DB207的导通时间与参数选择

Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing

Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design

Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability

2019阿里集群数据集使用总结

I want to say more about this communication failure

78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO

10 advanced concepts that must be understood in learning SQL

The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement

【Swoole系列2.1】先把Swoole跑起来
随机推荐
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
I want to say more about this communication failure
Appium automated test scroll and drag_ and_ Drop slides according to element position
D binding function
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
Pourquoi Li shufu a - t - il construit son téléphone portable?
2022暑期项目实训(一)
MarkDown语法——更好地写博客
2022 Summer Project Training (I)
微信小程序中给event对象传递数据
MSF横向之MSF端口转发+路由表+SOCKS5+proxychains
Pytest learning ----- pytest confitest of interface automation test Py file details
STM32 key state machine 2 - state simplification and long press function addition
Jerry's watch reading setting status [chapter]
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
ASEMI整流桥DB207的导通时间与参数选择