Skip to content
Home » C Programs » Quick Sort in C Program With Explanation

Quick Sort in C Program With Explanation

Simple quick sort in c program with explanation. In this quick sort program in c for beginners in data structure concept in c.

Definition of Quick Sort in C

A sorting algorithm that divides a list into two pieces progressively, moving lower items to one side and higher things to the other. It starts by picking one item from the entire list to serve as the pivot point.

The sorting algorithm is used to search for information, and because Quicksort is the fastest algorithm, it is frequently employed as a superior method of searching. It’s utilised in situations where a stable sort isn’t necessary.

Temporary Memory in C Program

A temp variable is used in a C program to temporarily swap two integers or to assign any value. A temporary variable in computer programming is a variable with a brief lifetime, 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 brief lifetime, it is typically stated as a local variable, that is a variable having a limited scope. Temp memory is used to execute the quick sort in c program with explanation.

Quick Sort in C Program With Explanation

//quick sort in c program with explanation
#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 Elements56
78
34
After sorting 34 56 78