main.cpp
#include "stdio.h"
#include "stdlib.h"
#include "QuickSort.h"
void main ( )
{
int A[ 10 ] = { 0 , 5 , 6 , 7 , 8 , 3 , 2 , 9 , 4 , 3 } ;
QuickSort ( A, 10 ) ;
PrintfSort ( A, 10 ) ;
}
QuickSort.h
#ifndef QUICK
#include <stdio.h>
void QuickSort ( int * A, int N) ;
void PrintfSort ( int * A, int N) ;
#endif
#pragma once
QuickSort.cpp
#include "QuickSort.h"
void Swap ( int * a, int * b)
{
int temp ;
temp = * a;
* a = * b;
* b = temp;
}
int Median3 ( int * A, int left, int right)
{
int center = ( left + right) / 2 ;
if ( A[ left] > A[ center] )
{
Swap ( & A[ left] , & A[ center] ) ;
}
if ( A[ left] > A[ right] )
{
Swap ( & A[ left] , & A[ right] ) ;
}
if ( A[ center] > A[ right] )
{
Swap ( & A[ center] , & A[ right] ) ;
}
Swap ( & A[ center] , & A[ right - 1 ] ) ;
return A[ right - 1 ] ;
}
void Qsort ( int * A, int left, int right)
{
if ( left >= right)
{
return ;
}
int Pivot, low, high;
Pivot = Median3 ( A, left, right) ;
low = left;
high = right - 1 ;
if ( low >= high)
{
return ;
}
while ( 1 )
{
while ( A[ ++ low] < Pivot)
{
}
while ( A[ -- high] > Pivot)
{
}
if ( low < high)
{
Swap ( & A[ low] , & A[ high] ) ;
}
else
{
break ;
}
}
if ( right - 1 )
{
}
Swap ( & A[ low] , & A[ right - 1 ] ) ;
Qsort ( A, left, low - 1 ) ;
Qsort ( A, low + 1 , right) ;
}
void QuickSort ( int * A, int N)
{
Qsort ( A, 0 , N - 1 ) ;
}
void PrintfSort ( int * A, int N)
{
for ( int i = 0 ; i < N; i++ )
{
printf ( "%d\n" , A[ i] ) ;
}
}