I'm learning recursion , so i'm trying to create a program that reverse a number in array recursively and using divide and conquer technique (i'm not sure this is divide and conquer or not), so what my problem is, i want to know why if i delete line 11 the program still work properly, but if i delete line 16, it will goes infinitely.
And i checked through my debugger, i know why it's infinite loop, because each stacked frames index left is still less than right, this makes the loop goes infinitely, so my question, why recursive call start from check the while condition and skip the line 11? this is weird for me because i just learn some basics of recursive, so this is abit confusing for me. so in my code, the line 16 is considered as the base case? because i learn from tutorial that recursive need a base case.
#include <stdio.h>
#define size 10
void swap(int *a, int *b)
{
int temp= *b;
*b = *a;
*a = temp;
}
void revcur (int arr[], int left, int right)
{
if (left>=right) return; //This program still works even if i delete this line or comment
while (left<right)
{
swap(&arr[left],&arr[right]);
revcur(arr,left+1,right-1);
return; //This program will go to infinite recursive if i delete this
}
}
int main()
{
int arr[size]={1,2,3,4,5,6,7,8,9,10};
revcur(arr,0,size-1);
int i; for (i=0; i<size; i++)
{
printf("%d ",arr[i]);
}
}