当前位置:网站首页>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
边栏推荐
- F200——搭载基于模型设计的国产开源飞控系统无人机
- Dichotomy (integer dichotomy, real dichotomy)
- 10 advanced concepts that must be understood in learning SQL
- 编译原理——预测表C语言实现
- 推荐好用的后台管理脚手架,人人开源
- declval(指导函数返回值范例)
- 8位MCU跑RTOS有没有意义?
- FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
- OpenEuler 会长久吗
- 【Android】Kotlin代码编写规范化文档
猜你喜欢
Compilation Principle -- C language implementation of prediction table
微信为什么使用 SQLite 保存聊天记录?
Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
【.NET CORE】 请求长度过长报错解决方案
Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
Jerry's updated equipment resource document [chapter]
Alibaba brand data bank: introduction to the most complete data bank
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
随机推荐
Awk command exercise
Getting started with pytest ----- test case pre post, firmware
關於這次通信故障,我想多說幾句…
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
The shell generates JSON arrays and inserts them into the database
Interview assault 63: how to remove duplication in MySQL?
STM32 key state machine 2 - state simplification and long press function addition
ADB common commands
Reprint: defect detection technology of industrial components based on deep learning
std::true_type和std::false_type
Insert dial file of Jerry's watch [chapter]
Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
2022暑期项目实训(一)
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
OliveTin能在网页上安全运行shell命令(上)
I want to say more about this communication failure
Jerry's updated equipment resource document [chapter]