Quick Sort is a widely used sorting algorithm that follows the divide-and-conquer paradigm. Here’s a simple implementation of the Quick Sort algorithm in the C programming language
Program :
#include <stdio.h>
#include <conio.h>
Â
int split ( int*, int, int ) ;
Â
void main( )
{
int arr[100];
int i,n;
printf(“Enter the Number of Terms : “);
scanf(“%d”,&n);
printf(“Enter the Elements of the Array : “);
for(i=1;i<=n;i++)
{
Â
scanf(“%d”,&arr[i]);
}
void quicksort ( int *, int, int ) ;
Â
printf ( “Quick sort.\n” ) ;
printf ( “\nArray before sorting:\n”) ;
Â
for ( i = 1 ; i <= n ; i++ )
printf ( “%d\t”, arr[i] ) ;
Â
quicksort ( arr, 0, n ) ;
Â
printf ( “\nArray after sorting:\n”) ;
Â
for ( i = 1 ; i <= n ; i++ )
printf ( “%d\t”, arr[i] ) ;
Â
getch( ) ;
}
Â
void quicksort ( int a[ ], int lower, int upper )
{
int i ;
if ( upper > lower )
{
i = split ( a, lower, upper ) ;
quicksort ( a, lower, i – 1 ) ;
quicksort ( a, i + 1, upper ) ;
}
}
Â
int split ( int a[ ], int lower, int upper )
{
int i, p, q, t ;
Â
p = lower + 1 ;
q = upper ;
i = a[lower] ;
Â
while ( q >= p )
{
while ( a[p] < i )
p++ ;
Â
while ( a[q] > i )
q– ;
Â
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
Â
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
Â
return q ;
}
Â