当前位置:网站首页>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
边栏推荐
- 【Android】Kotlin代码编写规范化文档
- node の SQLite
- 最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年
- Manifest of SAP ui5 framework json
- Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
- On time and parameter selection of asemi rectifier bridge db207
- Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
- The difference between parallelism and concurrency
- Pytorch extract middle layer features?
- 转载:基于深度学习的工业品组件缺陷检测技术
猜你喜欢
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
30 分钟看懂 PCA 主成分分析
sql语句优化,order by desc速度优化
Getting started with pytest ----- test case pre post, firmware
Jerry's updated equipment resource document [chapter]
Distinguish between basic disk and dynamic disk RAID disk redundant array
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
UDP协议:因性善而简单,难免碰到“城会玩”
微信为什么使用 SQLite 保存聊天记录?
随机推荐
ASEMI整流桥DB207的导通时间与参数选择
adb常用命令
Interview shock 62: what are the precautions for group by?
【Swoole系列2.1】先把Swoole跑起来
Interview shock 62: what are the precautions for group by?
MySQL 8 sub database and table backup database shell script
C language exchanges two numbers through pointers
Getting started with pytest ----- test case pre post, firmware
Why should Li Shufu personally take charge of building mobile phones?
VR全景婚礼,帮助新人记录浪漫且美好的场景
Jielizhi obtains the customized background information corresponding to the specified dial [chapter]
2019阿里集群数据集使用总结
关于这次通信故障,我想多说几句…
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
Pytest learning ----- detailed explanation of the request for interface automation test
F200——搭载基于模型设计的国产开源飞控系统无人机
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
SQL statement optimization, order by desc speed optimization
8位MCU跑RTOS有没有意义?
【剑指 Offer】 60. n个骰子的点数