C Program to Implement Quick Sort of a Given Numbers

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 ;
}

 

Output ;

Leave a Comment

Your email address will not be published.

Shopping Basket
Verified by MonsterInsights