I have always been taught and read recursive methods are slower than iterative methods because recursion requires the allocation of a new stack frame. I remember this was one of the obvious differences of these two.
But, in the following C programs, I'm seeing that the recursive function is faster than the iterative one.!!!
/****Bubble Sort (by iteration)****/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/****Bubble Sort Function declaration****/
void sort(int *,int);
/****Main Function****/
int main()
{
int n,*arr,i;
float exetime;
clock_t cstart,cend=0;
printf("Enter the number of elements: ");
scanf("%d",&n);
arr=malloc(n*sizeof(int));
printf("Enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
cstart=clock(); //starting time
for(i=0;i<10000;i++)
sort(arr,n);
cend=clock(); //end time
exetime=(float)(cend-cstart)/CLOCKS_PER_SEC;
printf ("%.4f \xC2\xB5sec\n",exetime*100);
for(i=0;i<n;i++)
printf("%-2d",arr[i]);
free(arr);
return 0;
}
/****Bubble Sort Function****/
void sort(int *arr,int n)
{
int i,j,temp;
for(i=0;i<=n-2;i++)
for(j=0;j<=n-2-i;j++)
if(arr[j+1]<arr[j])
{
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
output:
Enter the number of elements: 5
Enter the array elements: 5 4 3 2 1
0.1262 µsec
1 2 3 4 5
/****Bubble Sort (by recursion)****/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/****Bubble Sort Function declaration****/
void sort(int *,int);
/****Main Function****/
int main()
{
int n,*arr,i;
float exetime;
clock_t cstart,cend=0;
printf("Enter the number of elements: ");
scanf("%d",&n);
arr=malloc(n*sizeof(int));
printf("Enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
cstart=clock(); //starting time
for(i=0;i<10000;i++)
sort(arr,n);
cend=clock(); //end time
exetime=(float)(cend-cstart)/CLOCKS_PER_SEC;
printf ("%.4f \xC2\xB5sec\n",exetime*100);
for(i=0;i<n;i++)
printf("%-2d",arr[i]);
free(arr);
return 0;
}
/****Bubble Sort Function****/
void sort(int *arr,int n)
{
static int i=0;
int j,temp;
for(j=0;j<=n-2-i;j++)
if(arr[j+1]<arr[j])
{
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
if(++i<=n-2)
sort(arr,n);
}
output:
Enter the number of elements: 5
Enter the array elements: 5 4 3 2 1
0.0227 µsec
1 2 3 4 5