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

C Program to Implement Quick Sort Using Recursion

Basic c program to implement quick sort using recursion. In this program we can execute the quicksort program using recursion for beginners in c programming language.

Quick Sort Program Explanation

A sorting algorithm that gradually divides a list into two parts, moving lower items to one side and higher items to the other. It begins by selecting one item from the entire list as the pivot point.

The sorting algorithm is used to find information, and because Quicksort is the fastest, it is frequently used as a superior method of searching. It is used in situations where a stable type is not required.

Temporary Memory in C

In a C program, a temp variable is used to temporarily swap two integers or to assign any value. In computer programming, a temporary variable is a variable with a short lifetime that is typically used to retain data that will be deleted quickly or before it can be placed in a more permanent memory location. Because it has a short lifetime, it is commonly referred to as a local variable, which is a variable with a limited scope.

Pivot Element in C Program

Quicksort chooses a pivot from the collection, which is a somewhat arbitrary element. After that, the pivot point is used to partition (or divide) the larger unsorted collection into two smaller lists. 

The name “Quick Sort” comes from the fact that it can sort a list of data elements significantly faster (twice or three times faster) than any other sorting algorithm. As a result, quick sort is also known as “Partition Exchange” sort. Pivot element is used to execute the c program to implement quick sort using recursion.

C Program to Implement Quick Sort Using Recursion

//c program to implement quick sort using recursion 
#include<stdio.h>
void sort(int number[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
sort(number,first,j-1);
sort(number,j+1,last);
}
}
int main()
{
int i, count, number[25];
printf("How Many Elements Do You Want");
scanf("%d",&count);
printf("Enter %d Elements", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
sort(number,0,count-1);
printf("After Sorting");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}

Output:

How Many Elements Do You Want3
Enter 3 Elements45
23
45
After Sorting 23 45 45