Skip to content
Home » C Programs » Quick Sort Program in C Using Recursion

Quick Sort Program in C Using Recursion

Basic quick sort program in c using recursion. In this program, we can execute the quicksort program in c using recursion for beginners to understand the basic concepts in c programming.

Quick Sort Definition in C

A sorting method that sequentially divides a list into two sections and moves the lower items to one side and the higher ones to the other. It begins by selecting one item from the full list to act as the pivot point. 

The sorting algorithm is used to search for information, and because Quicksort is the fastest algorithm, it is commonly utilized as a better approach to search. It is used anywhere a stable sort is not required.

Recursion Definition in C

Recursion is a process that occurs when a function calls a copy of itself to work on a lesser problem. Any function that calls itself is referred to as a recursive function, and such function calls are referred to as recursive calls. Recursion entails a large number of recursive calls. Here we have executed the quick sort program in c using recursion 

Quick Sort Program in C Using Recursion

#include <stdio.h>
void quicksort (int [], int, int);
int main()
{
int list[50];
int size, i;
printf("Enter the Number of Elements ");
scanf("%d", &size);
printf("\nNumber Which are going to sorted\n");
for (i = 0; i < size; i++)
{
printf("\nEnter [ %d ] element :: ",i+1);
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("\n 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);
}
}

Number Which are going to sorted

Enter [ 1 ] element :: 67

Enter [ 2 ] element :: 34

Enter [ 3 ] element :: 23

Enter [ 4 ] element :: 45

Enter [ 5 ] element :: 67

Sorted List is

23 34 45 67 67