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

边栏推荐
- 2022 Summer Project Training (I)
- Jielizhi obtains the currently used dial information [chapter]
- 开源与安全的“冰与火之歌”
- Jerry's updated equipment resource document [chapter]
- Jerry is the custom background specified by the currently used dial enable [chapter]
- 8位MCU跑RTOS有没有意义?
- There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
- JMeter interface test response data garbled
- 1700C - Helping the Nature
- 2019阿里集群数据集使用总结
猜你喜欢

Take you through ancient Rome, the meta universe bus is coming # Invisible Cities

scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月

What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?

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

Windows连接Linux上安装的Redis

李书福为何要亲自挂帅造手机?

Compilation Principle -- C language implementation of prediction table

declval(指导函数返回值范例)

Getting started with pytest ----- test case pre post, firmware

C language exchanges two numbers through pointers
随机推荐
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
Appium automated test scroll and drag_ and_ Drop slides according to element position
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
Transfer data to event object in wechat applet
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
【.NET CORE】 请求长度过长报错解决方案
开源与安全的“冰与火之歌”
Pourquoi Li shufu a - t - il construit son téléphone portable?
Pytest learning ----- pytest confitest of interface automation test Py file details
ASEMI整流桥DB207的导通时间与参数选择
Compilation principle - top-down analysis and recursive descent analysis construction (notes)
高精度运算
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Principle and usage of extern
MySQL 8 sub database and table backup database shell script
Interesting - questions about undefined
F200 - UAV equipped with domestic open source flight control system based on Model Design