主题:还有一个问题,这个比较简单
如何统计递归函数的调用次数
程序如下
#include<stdio.h>
#include <conio.h>
#include <ctype.h>
#include<iostream>
using namespace std;
void q(int a[],int low,int up)
{
int i,j;
int t;
if(low<up)
{
i=low;
j=up;
t=a[low];
while(i!=j)
{
while(i<j&&a[j]>t)
j--;
if(i<j)a[i++]=a[j];
while(i<j&&a[i]<=t)
i++;
if(i<j)a[j++]=a[i];
}
a[i]=t;
q(a,low,i-1);
q(a,i+1,up);
}
}
main()
{
int a[1000];
int n;
int j;
printf("要输入的数组个数=");
cin>>n;
for(j=0;j<n;j++)
{
printf("a[%d]=",j);
scanf("%d",&a[j]);
}
q(a,0,n-1);
for(j=0;j<n;j++)
{
printf("a[%d]=",j);
printf("%d\n",a[j]);
}
}
程序如下
#include<stdio.h>
#include <conio.h>
#include <ctype.h>
#include<iostream>
using namespace std;
void q(int a[],int low,int up)
{
int i,j;
int t;
if(low<up)
{
i=low;
j=up;
t=a[low];
while(i!=j)
{
while(i<j&&a[j]>t)
j--;
if(i<j)a[i++]=a[j];
while(i<j&&a[i]<=t)
i++;
if(i<j)a[j++]=a[i];
}
a[i]=t;
q(a,low,i-1);
q(a,i+1,up);
}
}
main()
{
int a[1000];
int n;
int j;
printf("要输入的数组个数=");
cin>>n;
for(j=0;j<n;j++)
{
printf("a[%d]=",j);
scanf("%d",&a[j]);
}
q(a,0,n-1);
for(j=0;j<n;j++)
{
printf("a[%d]=",j);
printf("%d\n",a[j]);
}
}