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

边栏推荐
- How to output special symbols in shell
- 重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
- Olivetin can safely run shell commands on Web pages (Part 1)
- Growth of operation and maintenance Xiaobai - week 7
- Why should Li Shufu personally take charge of building mobile phones?
- 【剑指 Offer】 60. n个骰子的点数
- 趣-关于undefined的问题
- The difference between parallelism and concurrency
- Smart street lamp based on stm32+ Huawei cloud IOT design
- 偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
猜你喜欢

Manifest of SAP ui5 framework json

编译原理——预测表C语言实现

Pytest learning ----- detailed explanation of the request for interface automation test

The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?

Olivetin can safely run shell commands on Web pages (Part 1)
![[Android] kotlin code writing standardization document](/img/d5/53d6a75e87af15799bf7e5d6eb92a5.png)
[Android] kotlin code writing standardization document

sql语句优化,order by desc速度优化
![Jerry is the custom background specified by the currently used dial enable [chapter]](/img/32/6c22033bda8ff1b53993bacef254cd.jpg)
Jerry is the custom background specified by the currently used dial enable [chapter]

Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
![Jerry's access to additional information on the dial [article]](/img/a1/28b2a5f7c16cbcde1625a796f0d188.jpg)
Jerry's access to additional information on the dial [article]
随机推荐
队列的实现
Dichotomy (integer dichotomy, real dichotomy)
Getting started with pytest ----- test case pre post, firmware
2019阿里集群数据集使用总结
OpenEuler 会长久吗
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
Markdown grammar - better blogging
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
李书福为何要亲自挂帅造手机?
Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
Interview assault 63: how to remove duplication in MySQL?
adb常用命令
Running the service with systemctl in the container reports an error: failed to get D-Bus connection: operation not permitted (solution)
78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
Insert dial file of Jerry's watch [chapter]
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
Getting started with pytest ----- allow generate report
Codeforces Round #803 (Div. 2)