当前位置:网站首页>Application of recursion
Application of recursion
2022-07-19 05:30:00 【Learn to put down ta】
recursive
Function recursion is the function calling itself , To set exit conditions , Otherwise, it will run until the memory space is exhausted
The application of recursion 1
Hanoi
Stick it for a while python Written Hanoi Tower code , The mind is the same , Smart you will definitely write him C Language code , So I won't post code .
Hanoi Tower code implementation
The application of recursion 2
Quick sort
The basic idea : Divide and rule
Find a benchmark value ( Just order one , Habit is the number in the middle of the array ), Find the number larger than the reference point from left to right (i It's a subscript ) And looking for a number smaller than the reference point from right to left (j It's a subscript ), Then exchange the two numbers , When i exceed j To exit the loop , At this point, the array is divided into two segments , The left is smaller than the right , Then recurse the arrays at both ends until each array has only one element .
Test code :
void quick_sort(int array[],int left,int right);
void quick_sort(int array[],int left,int right)
{
int i = left,j = right;// Traversal array
int temp;// The cornerstone of exchange
int qovit;// Selected reference value
qovit = array[(left + right)/2];
while(i<=j)// Go through the array
{
while(array[i]<qovit)// Select the number on the left that is greater than the reference value to jump out of the loop
{
i++;
}
while(array[j]>qovit)// Select the number on the right that is greater than the reference value to jump out of the cycle
{
j--;
}
if(i<j)// Exchange the two numbers selected above
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;// Note here that the value of the indicator should be changed after the exchange , Otherwise, there is no circulation , Don't ask me how I know
j--;
}
}
if(left<j)
{
quick_sort(array,left,j);// Call quick sort recursively on the left
}
if(i<right)
{
quick_sort(array,i,right);// Call quick sort recursively on the right
}
}
int main(void)
{
int array[]={
111, 21, 32, 55, 2, 543, 520, 11, 23, 88};
int length = sizeof(array) / sizeof(array[0]);
quick_sort(array,0,length-1);
printf(" The result of sorting is :");
for(int i=0;i < length;i++)
{
printf("%d ",array[i]);
}
return 0;
}
result :
边栏推荐
猜你喜欢

操作系統常見面試題

How to deal with the mismatch between subtitle files and video files

使用Flink SQL传输市场数据1:传输VWAP

ArcMap creates a constant grid and tessellates it into a new grid

【全网首发】JVM性能问题的自动分析
![[AI] action recognition using simple neural network -- Based on coco key points](/img/67/cd6be6e070fb5d4d44ee043ebd7fac.png)
[AI] action recognition using simple neural network -- Based on coco key points

Round robin schedule problem

一次全面的性能优化,从5秒优化到1秒

2022年春招最新消息:IT互联网行业平均薪资18500元

7.数据仓库搭建之数据仓库环境准备
随机推荐
Implementation of synchronization interface of 6 libcurl based on libco
jvm学习
【全网首发】JVM性能问题的自动分析
MySQL学习笔记(4)——(基本CRUD)操作数据库中的表的数据
线上软件测试培训机构柠檬班与iTest.AI平台达成战略合作
MySQL - index
Log4j的使用
Swagger configuration and use
sql时间对比
mysql的事务
Solve the problem of inconsistent prediction effect between text detection training model and information model based on paddleocr
MYSQL基本语法字典
Common methods of goframe error handling & use of error codes
Solutions for vscode terminal failure
聊聊并发编程的12种业务场景
mysql的缓存方案问题解决
Two or three things to know about operation and maintenance safety
2.6.2 内存泄漏
Rxjs源码解析(一)Observable
[first launch in the whole network] automatic analysis of JVM performance problems