Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.
Problem : :
The following C program, using recursion, performs quick sort. A quick sort is a sorting algorithm with complexity of O( nlogn ). It is used for sorting numbers, structure, files.
Here is the source code of the C Program to implement Quick Sort using recursion. The C Program is successfully compiled and run on a Windows system. The program output is also shown below.
SOURCE CODE :
/* C Program to implement Quick Sort using recursion */
#include <stdio.h>
void quicksort (int [], int, int);
int main()
{
int list[50];
int size, i;
printf("How many elements u want to Sort :: ");
scanf("%d", &size);
printf("\nEnter the elements below to be sorted :: \n");
for (i = 0; i < size; i++)
{
printf("\nEnter [ %d ] element :: ",i+1);
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("\nAfter implementing Quick sort, Sorted List is :: \n\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return 0;
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (list[i] <= list[pivot] && i <= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low)
{
j--;
}
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
}
Output : :
/* C Program to implement Quick Sort using recursion */
How many elements u want to Sort :: 8
Enter the elements below to be sorted ::
Enter [ 1 ] element :: 3
Enter [ 2 ] element :: 1
Enter [ 3 ] element :: 5
Enter [ 4 ] element :: 2
Enter [ 5 ] element :: 8
Enter [ 6 ] element :: 0
Enter [ 7 ] element :: 6
Enter [ 8 ] element :: 9
After implementing Quick sort, Sorted List is ::
0 1 2 3 5 6 8 9
Process returned 0
Above is the source code for C Program to implement Quick Sort using recursion which is successfully compiled and run on Windows System.The Output of the program is shown above .
Recursion : :
Problem : :
The following C program, using recursion, performs quick sort. A quick sort is a sorting algorithm with complexity of O( nlogn ). It is used for sorting numbers, structure, files.
Here is the source code of the C Program to implement Quick Sort using recursion. The C Program is successfully compiled and run on a Windows system. The program output is also shown below.
SOURCE CODE :
Output : :
Above is the source code for C Program to implement Quick Sort using recursion which is successfully compiled and run on Windows System.The Output of the program is shown above .
need an explanation for this answer? contact us directly to get an explanation for this answer