当前位置:网站首页>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
边栏推荐
- std::true_ Type and std:: false_ type
- IP, subnet mask, gateway, default gateway
- Introduction to the usage of model view delegate principal-agent mechanism in QT
- Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
- 30 分钟看懂 PCA 主成分分析
- declval(指导函数返回值范例)
- 酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅
- Pytorch extract middle layer features?
- Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
- Manifest of SAP ui5 framework json
猜你喜欢
Jerry's updated equipment resource document [chapter]
In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way
模板于泛型编程之declval
STM32按键状态机2——状态简化与增加长按功能
[Android] kotlin code writing standardization document
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
李書福為何要親自掛帥造手機?
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅
declval(指导函数返回值范例)
随机推荐
C language exchanges two numbers through pointers
IP, subnet mask, gateway, default gateway
微信为什么使用 SQLite 保存聊天记录?
Jerry's access to additional information on the dial [article]
Codeforces Round #803 (Div. 2)
MSF horizontal MSF port forwarding + routing table +socks5+proxychains
Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
STM32按键状态机2——状态简化与增加长按功能
MSF横向之MSF端口转发+路由表+SOCKS5+proxychains
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
2019 Alibaba cluster dataset Usage Summary
OliveTin能在网页上安全运行shell命令(上)
Jerry's watch reads the file through the file name [chapter]
Alibaba brand data bank: introduction to the most complete data bank
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
Nodejs developer roadmap 2022 zero foundation Learning Guide
Awk command exercise
How to output special symbols in shell
【剑指 Offer】 60. n个骰子的点数
面试突击62:group by 有哪些注意事项?