当前位置:网站首页>Sorting method (3) = > quick sort and merge sort
Sorting method (3) = > quick sort and merge sort
2022-04-21 07:38:00 【Yuxuan won't be bald】
4. Quick sort
Quick sort is called quick sort for short , It mainly uses the main idea of recursion
recursive
Recursion is a method that calls its own method
Recursion mainly includes Recursive relation and recursive exit
example :
① Array {1,2,3,4,5,…,n}
Recursive export : When n=1 when , The return value is n;
Recursion : At other times , The return value is (n-1)+1;
Code :
public static int run(int n) {
if(n==1)
return 1;// Recursive export
return run(n-1)+1;// Recursion
}
② Fibonacci sequence
{1,1,2,3,5,8,13,…}
The third term is equal to the sum of the first two terms
Recursive export : When n=1,n=2 when , The return values are 1;
Recursion : In other cases , Returns the sum of the first two items , That is to say F(n-1)+F(n-2);
Code :
public static int FI(int n) {
if(n==1) {
return 1;
}
else if(n==2) {
return 1;// Recursive export
}
else
return FI(n-1)+FI(n-2);// Recursion
}
③ An array of former n Xiang He
Recursive export : When n=1 when , The output value is 1;
Recursion : Other situations , Before output (n-1) Item and plus n Item value , That is to say sum(n-1)+n
Code :
public static int sum(int n) {
if(n==1) {
return 1;// Recursive export
}
else
return sum(n-1)+n;// Recursion
}
recursive procedure :
① Take the first number on the left as the base ;
② Define two pointers in the first and last positions ;
③ The next pointer moves first , Then it's better than the benchmark number , stop it ;
④ The front pointer moves backward , Find a number larger than the benchmark number , stop it ;
⑤ Data interchange
Code :
public class QuickSort {
public static void main(String[] args) {
int[] arr=new int[] {5,9,2,1,7,6,3,4,8,0};
qucik(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
public static void qucik(int arr[],int left,int right) {
if(left>right) {
return;
}
int base=arr[left];
int i=left;
int j=right;
while(i!=j) {
while(arr[j]>=base && i<j) {
j--;
}
while(arr[i]<=base && i<j) {
i++;
}
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
arr[left]=arr[i];
arr[i]=base;
qucik(arr,left,i-1);
qucik(arr,i+1,right);
}
Result chart :

5. Merge sort
Merge sort uses Divide and conquer method The main idea of , Sort mainly by looking for intermediate variables
Code :
public static void main(String[] args){
int[] arr=new int[] {5,8,2,1,7,6,3,4,9,0};
}
public static void fen(int arr,int left, int right) {
if(left>right) {
return ;
}
int mid=(left + right)/2;
fen(arr,left,mid);
fen(arr,mid+1,right);
}
public static void he(int[] arr,int left, int right,int mid) {
int s1=left;
int s2=mid+1;
// Define a temporary array
int[] temp=new int[right-left+1];
int index=0;
// Put the small data in the two arrays into the temporary array
while(s1<mid && s2<=right) {
if(arr[s1]<=arr[s2]){
temp[index]=arr[s1];
index ++;
s1 ++;
}
else {
temp[index]=arr[s1];
index ++;
s2 ++;
}
}
while(s1<mid) {
temp[index]=arr[s1];
index ++;
s1 ++;
}
while(s1<right) {
temp[index]=arr[s1];
index ++;
s2 ++;
}
for(int j=0;j<temp.length;j++) {
arr[j+left]=temp[j];
}
}
Result chart :

版权声明
本文为[Yuxuan won't be bald]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210625096247.html
边栏推荐
- 蓝桥杯——A+B问题
- 搭建mysql主从复制、读写分离、一主一从
- Mmdetection uses custom dataset training to convert datasets to coco format
- 宏晶微MS9288、模拟RGB(VGA)转HDMI,HDMI发射器,音频解码器,内置MCU
- 龙讯系列: LT8911,LVDS/MIPI DSI信号转换成EDP信号的转接芯片,LT8619,HDMI/MHL信号转换成LVDS/RGB信号
- File system structure analysis and data recovery
- 任意密码修改
- MS12_ 020 vulnerability
- Voisins bgp
- MySQL simple operation statement
猜你喜欢

Ms12 020 vulnérabilité

Ms1836s, HDMI to CVBS, video converter, HDMI receiver, built-in MCU and memory

Echars control legend all or none - simple case

Nodered connection database

Longxun series: lt8912b, lt6911c, HDMI to Mipi (CSI \ DSI), single channel mipi2 DSi bridge to LVDS / HDM, hdmi1 4 to dual port mipidsi/csi and audio data sheet

Ruiyuan ry8132 and ry9140 DCDC are mainly replaced by tps563200 and tps563201 of Ti, mp1471 and mp1653 of Xinyuan, and details of sy8104 power chip of silijie

Domestic first USBhub Daquan, USB hub2 0,HUB3. 0, Wangjiu prolific, pl2586, ma8601, and xinrunde SL2 2A、SL2. 2s, replace Tang Ming's Fe1 1、FE1. 1s,, Weifeng vl810

未授权访问漏洞
![[intensive reading] deep surface normal estimation with hierarchical rgb-d fusion](/img/50/ccc038ac068ae7c51ae53c2dcf3df9.png)
[intensive reading] deep surface normal estimation with hierarchical rgb-d fusion

UFIDA OA vulnerability reproduction manual
随机推荐
蓝桥杯——回文数与特殊回文数
IPV4-IGP
Weak password-20211221
[hand pose estimation] [paper accuracy] pose guided structured region ensemble network for cascaded hand pose estimation
JS不同时间格式转换
【无标题】国腾GM系列,GM8284DD(GM8284DR,LT8218A)、 GM8285C、GM7123C,LVDSTTL转TTL,TTL转成单路LVDS,TTL数字信号转换成VGA
BGP automatic route aggregation
宏晶微MS9123,USB 投屏控制芯片,USB 投屏器,支持 CVBS、S-Video 视频接口,
jfinal框架easyexcel插件导出带图片
Log4j Remote Code Execution Vulnerability verification
Macro crystal micro ms9288, analog RGB (VGA) to HDMI, HDMI transmitter, audio decoder, built-in MCU
Ms12 020 vulnérabilité
ConvergenceWarning: Liblinear failed to converge, increase the number of iterations解决辦法
Basic concepts of IP Multicast
国产POE,RPC304,替换IP802、IP804、IP808,替换TPS23861、LTC4292、PD69204、SI3459,国产首发POE供电、PSE,睿普康国产以太网供电及接口转换芯片
Use Baidu map POI to crawl the required data
oss上传
Detailed explanation of the whole evolution process and architecture design of large websites
word xml 空格符
蕊源电源芯片,RY3715,RY3750替换:矽力杰SY7208、SY7152、芯朋微AP2008.芯源MP1542,MP3213。2.5V至5.5V的输入电压