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;
}
}
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);
}
Enter the number of elements: 5
Enter the array elements: 5 4 3 2 1
0.0227 µsec
1 2 3 4 5
You're comparing apples to oranges here. Your first algorithm is an iterative bubble sort, and the second is a recursive selection sort. (It's labeled as a bubble sort, but it's actually not. Notice that it works by finding the minimum value and swapping it to the front of the array.) Since you're looking at two totally different algorithms, it's not necessarily meaningful to draw any conclusions about recursion versus iteration.
I think it's also worth pointing out the particular recursive implementation of selection sort that you're using is tail-recursive (there's only one recursive call, and it's at the very end of the function.) Many compilers are smart enough to optimize tail recursion so that no recursion is involved at all and the code is converted to use loops. In other words, you might not even be seeing the cost of setting up and tearing down stack frames here.
Finally, while you're correct that recursive algorithms often incur a cost from setting up and tearing down stack frames, that doesn't necessarily mean that recursive algorithms are always inherently slower than iterative algorithms. In some cases, iterative versions of algorithms that are naturally recursive work by maintaining an explicit stack and performing operations on it. Depending on the implementation of the stack, that stack might end up needing to dynamically allocate memory in the heap, which is typically more expensive than setting up and tearing down stack frames. As always, if you have two algorithms and want to compare their empirical performance, it's probably best to just run the two against one another and see which ones win.
Hope this helps!