#include <stdlib.h>
#include <stdio.h>
typedef int ElementType;
void swap( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
ElementType middian( ElementType a[], int start, int end )
{
int center = ( start + end ) / 2;
if( a[start] > a[center] )
swap( &a[start], &a[center] );
if( a[start] > a[end] )
swap( &a[start], &a[end] );
if( a[center] > a[end] )
swap( &a[center], &a[end] );
swap( &a[center], &a[end-1] );
return a[end-1];
}
void sort( ElementType a[],int start, int end )
{
ElementType temp;
if( start >= end ) return;
int i, j;
for( i=start+1; i<=end; i++)
{
temp = a[i];
for( j=i; j>start; j--)
{
if( a[j-1]>temp )
a[j] = a[j-1];
else
break;
}
a[j] = temp;
}
}
void Qsort( ElementType a[], int start, int end )
{
ElementType pivot;
int i, j;
if( start + 3 < end )
{
pivot = middian( a, start, end );
i = start;
j = end-1;
for( ; ; )
{
while( a[++i] < pivot ) ;
while( a[--j] > pivot ) ;
if( i < j )
swap( &a[i], &a[j] );
else
break;
}
swap( &a[end-1], &a[i] );
Qsort( a, start, j );
Qsort( a, i+1, end );
}
else
sort( a, start, end );
}
int main()
{
ElementType b[20] = {12,24,48,26,47,52,13,486,426,14,
28,476,264,452,57,19,49,26,475,954};
int i;
Qsort( b, 0, 19 );
for( i=0; i<20; i++ )
printf("%d\t",b[i]);
sort( b, 0, 19 );
for( i=0; i<20; i++ )
printf("%d\t",b[i]);
return 0;