Implement Quick sort using recursion
In this post we will see how to implement quick sort using recursion in c/c++. Quick sort is simple and efficient algorithm for sorting items in array or elements in array. Its is very common algorithm used for sorting and asked in interviews as well.
Features of Quick sort
- Worst-case performance: O(n2)
- Average performance: O(n log n)
- Best-case performance: O(n log n)
Quick sort using recursion work is such as way that we need to select a pivot element. Now reorder the elements which are less than pivot should come before pivot. Similarly elements which are greater than pivot will come after selected pivot element. We need to repeat this process recursively on the element array.
Beyond this below implementation we have many different types of quick sorts available which are more efficient is specific scenarios.
Quick sort source code
#include
#include
void quick_sort(int [],int,int);
int partition(int [],int,int);
void main()
{
int a[30],n,i,op;
clrscr();
do
{
printf("\n1)Quick Sort\n2)Quit");
printf("\nEnter your choice : ");
scanf("%d",&op);
if(op==1)
{
printf("\nEnter no of elements :");
scanf("%d",&n);
printf("\nEnter array elements :");
for(i=0;i<=u);do
{ j--;
}while(a[j]>v);
if(i)>;i++)>
Quick sort using recursion Output
1)Quick Sort
2)Quit
Enter your choice : 1
Enter no of elements :6
Enter array elements :60 33 70 84 21 50
Sorted array is :21 33 50 60 70 84
1)Quick Sort
2)Quit
Enter your choice : 2
);>)>)>;i++)>
Leave a Reply