当前位置:网站首页>递归的方式
递归的方式
2022-07-06 10:08:00 【848698119】
递归的方式实现取数组最大值
@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]范围上求最大值
public int recursion(int[] arr, int l, int r) {
if (l == r) {
//arr[l...r]范围上只有一个数,直接返回
return arr[l];
}
int mid = l + ((r - l) >> 1); //求中点,防止溢出
int leftMax = recursion(arr, l, mid);
int rightMax = recursion(arr, mid + 1, r);
return Math.max(leftMax, rightMax);
}
归并排序

- 其用到的核心思想
分治
先局部排序,,,,,,然后整体排序
先把一个完整的数组分为两半(左一半,右一半,然后再把左边分成两半,,,,,直到不能再分,然后排序,,,,左边排,右边排。。。)最后整体排序
空间换时间
时间复杂度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]);
}
}
/** * 递归,二分排序 * @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); //左边有序
process(arr, mid + 1, r); //右边有序
merge(arr, l, mid, r);
}
/*** * 排序 * @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]; //把局部排好序的数组存放进来
int i = 0; //用于新数组的索引
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++];
}
/** * 把排好序的新数组赋给老数组 */
for (i = 0; i < help.length; i++) {
arr[l+i] = help[i];
}
}
归并排序的扩展

边栏推荐
- MarkDown语法——更好地写博客
- C语言通过指针交换两个数
- Getting started with pytest ----- test case pre post, firmware
- Debug and run the first xv6 program
- Nodejs 开发者路线图 2022 零基础学习指南
- MSF horizontal MSF port forwarding + routing table +socks5+proxychains
- 2022年大厂Android面试题汇总(一)(含答案)
- Zen integration nails, bugs, needs, etc. are reminded by nails
- 中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
- 编译原理——预测表C语言实现
猜你喜欢

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

Establishment of graphical monitoring grafana

Smart street lamp based on stm32+ Huawei cloud IOT design

C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例

Optimization of middle alignment of loading style of device player in easycvr electronic map

Zen integration nails, bugs, needs, etc. are reminded by nails

传统家装有落差,VR全景家装让你体验新房落成效果

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

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

趣-关于undefined的问题
随机推荐
二分(整数二分、实数二分)
std::true_type和std::false_type
Codeforces Round #803 (Div. 2)
J'aimerais dire quelques mots de plus sur ce problème de communication...
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
Jerry's updated equipment resource document [chapter]
node の SQLite
趣-关于undefined的问题
开源与安全的“冰与火之歌”
EasyCVR电子地图中设备播放器loading样式的居中对齐优化
Binary search strategy
Appium automated test scroll and drag_ and_ Drop slides according to element position
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
FlutterWeb瀏覽器刷新後無法回退的解决方案
Pourquoi Li shufu a - t - il construit son téléphone portable?
Running the service with systemctl in the container reports an error: failed to get D-Bus connection: operation not permitted (solution)
Unity粒子特效系列-闪星星的宝箱
The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement