Algorithm/Pseudo-code For Quicksort:
/* Double-Click To Select Code */
function quicksort('array')
if length('array') ≤ 1
return 'array' // an array of zero or one elements is already sorted
select and remove a pivot value 'pivot' from 'array'
create empty lists 'less' and 'greater'
for each 'x' in 'array'
if 'x' ≤ 'pivot' then append 'x' to 'less'
else append 'x' to 'greater'
return concatenate(quicksort('less'), 'pivot', quicksort('greater'))
// two recursive calls
Here is an Image depicting Quick Sort:![]() |
| In-place partition in action on a small list. The boxed element is the pivot element, blue elements are less or equal, and red elements are larger. |
C Program For Quick Sort:
/* Double-Click To Select Code */
#include<stdio.h>
#include<conio.h>
void quick_sort(int arr[20],int,int);
void main()
{
int arr[20],n,i;
clrscr();
printf("Enter the number of elements in the Array: ");
scanf("%d",&n);
printf("\nEnter %d elements:\n\n",n);
for(i=0 ; i<n ; i++)
{
printf(" Array[%d] = ",i);
scanf("%d",&arr[i]);
}
quick_sort(arr,0,n-1);
printf("\nThe Sorted Array is:\n\n");
for(i=0 ; i<n ; i++)
{
printf(" %4d",arr[i]);
}
getch();
}
void quick_sort(int arr[20],int low,int high)
{
int pivot,j,temp,i;
if(low<high)
{
pivot = low;
i = low;
j = high;
while(i<j)
{
while((arr[i]<=arr[pivot])&&(i<high))
{
i++;
}
while(arr[j]>arr[pivot])
{
j--;
}
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
temp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=temp;
quick_sort(arr,low,j-1);
quick_sort(arr,j+1,high);
}
}
Output of Program:
Please Comment If You Liked This Post !!
This article uses material from the Wikipedia article Quick Sort which is released under the Creative Commons Attribution-Share-Alike License 3.0
Do you like this post? Please link back to this article by copying one of the codes below.
URL: HTML link code: Forum link code:

