# poj2388 水题 各种排序

//插入排序 79ms

#include <iostream>
using namespace std;

void InsertSort(int a[], int n)
{
int i,j,tmp;
for(i = 1; i < n; i++)
{
tmp = a[i];
for(j = i; j > 0 && a[j-1] > tmp; j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
int main()
{
int n,i;
int a[10002];

scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
InsertSort(a,n);
printf("%d\n",a[(n-1)/2]);
return 0;
}

//增量较差的希尔排序 32ms

#include <iostream>
using namespace std;

void ShellSort( int a[], int n )
{
int i,j,tmp,increment;

for( increment = n/2; increment > 0; increment /= 2 )
for( i = increment; i < n; i++ )
{
tmp = a[i];
for( j = i; j >= increment; j -= increment )
if( a[j-increment] > tmp )
a[j] = a[j-increment];
else break;
a[j] = tmp;
}
}
int main()
{
int n,i;
int a[10002];

scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
ShellSort(a,n);
printf("%d\n",a[(n-1)/2]);
return 0;
}

//Hibbard增量希尔排序 32ms

#include <iostream>
#include <cmath>
using namespace std;
int Hibbard[15];

void ShellSort( int a[], int n )
{
int i,j,k,tmp,increment;

for( k = 0; k < 15; k++ )
if( Hibbard[k] < n && Hibbard[k+1] >= n )
{
increment = Hibbard[k];
break;
}
for(; k >= 0; increment = Hibbard[--k] )
for( i = increment; i < n; i++ )
{
tmp = a[i];
for( j = i; j >= increment; j -= increment )
if( a[j-increment] > tmp )
a[j] = a[j-increment];
else break;
a[j] = tmp;
}
}
int main()
{
int n,i;
int a[10002];
for( i = 1; i < 15; i++ )
Hibbard[i-1] = pow(2.0,i+0.0)-1;

scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
ShellSort(a,n);
printf("%d\n",a[(n-1)/2]);
return 0;
}

//堆排序 16ms
#include <iostream>
using namespace std;
#define leftChild(i) 2*i+1

void Swap( int *a, int *b )
{
int hold = *b;

*b = *a;
*a = hold;
}
void PercDown( int a[], int i, int n )
{
int child,tmp;

tmp = a[i];
for(; leftChild(i) < n; i = child )
{
child = leftChild(i);
if( child != n-1 && a[child+1] > a[child] )
child++;
if( tmp < a[child] )
a[i] = a[child];
else break;
}
a[i] = tmp;
}
void HeapSort( int a[], int n )
{
int i;

for( i = n/2; i >= 0; i-- )
PercDown( a, i, n );
for( i = n-1; i > 0; i-- )
{
Swap( &a[0], &a[i] );
PercDown( a, 0, i );
}
}
int main()
{
int n,i;
int a[10002];

scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
HeapSort(a,n);
printf("%d\n",a[(n-1)/2]);
return 0;
}

//归并排序 32ms
#include <iostream>
#include <cstdlib>
using namespace std;

void Merge( int a[], int tmpArray[], int l_sta, int r_sta, int r_end )
{
int l_end = r_sta-1;
int size = r_end-l_sta+1;
int id = l_sta;

while( l_sta <= l_end && r_sta <= r_end )
if( a[l_sta] <= a[r_sta] )
tmpArray[id++] = a[l_sta++];
else
tmpArray[id++] = a[r_sta++];
while( l_sta <= l_end )
tmpArray[id++] = a[l_sta++];
while( r_sta <= r_end )
tmpArray[id++] = a[r_sta++];

for( int i = 0; i < size; i++,r_end-- )
a[r_end] = tmpArray[r_end];
}

void Msort( int a[], int tmpArray[], int left, int right )
{
int center;

if( left < right )
{
center = ( left+right )/2;
Msort( a, tmpArray, left, center );
Msort( a, tmpArray, center+1, right );
Merge( a, tmpArray, left, center+1, right );
}
}
void MergeSort( int a[], int n )
{
int *tmpArray;

tmpArray = (int*)malloc( n*sizeof(int) );
if( tmpArray != NULL )
{
Msort( a, tmpArray, 0, n-1 );
free( tmpArray );
}
}
int main()
{
int n,i;
int a[10002];

scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
MergeSort(a,n);
printf("%d\n",a[(n-1)/2]);
return 0;
}

//三数中值分割法实现的快排 47ms
#include <iostream>
using namespace std;
#define cutoff 3

void Swap( int *a, int *b )
{
int hold = *b;

*b = *a;
*a = hold;
}

void InsertSort(int a[], int n)
{
int i,j,tmp;
for(i = 1; i < n; i++)
{
tmp = a[i];
for(j = i; j > 0 && a[j-1] > tmp; j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
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 )
{
int i, j;
int Pivot;

if( left+cutoff <= right )
{
Pivot = Median3( a, left, right );
i = left;
j = right-1;
for( ; ; )
{
while( a[++i] < Pivot ) {}
while( a[--j] > Pivot ) {}
if( i < j )
Swap( &a[i], &a[j] );
else break;
}
Swap( &a[i], &a[right-1] );

Qsort( a, left, i-1 );
Qsort( a, i+1, right );
}
else InsertSort( a+left, right-left+1 );
}
int main()
{
int n,i;
int a[10002];

scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
Qsort( a, 0, n-1 );
printf("%d\n",a[(n-1)/2]);
return 0;
}
//以中间值为枢纽元的快排 16ms

#include <iostream>
#include <cstdio>
using namespace std;

void Swap( int &a, int &b )
{
int hold = b;

b = a;
a = hold;
}

void Qsort( int a[], int left, int right )
{
int i, j;
int mid;
int m;

if( left < right )
{
i = left - 1;
j = right;
mid = (left+right)/2;
m = a[mid];
Swap( a[mid], a[right] );
for( ; ; )
{
while( a[++i] < m ) {}
while( a[--j] > m ) {}
if( i < j )
Swap( a[i], a[j] );
else break;
}
Swap( a[i], a[right] );
Qsort( a, left, i-1 );
Qsort( a, i+1, right );
}
}
int main()
{
int n,i;
int a[10002];

scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
Qsort( a, 0, n-1 );
printf("%d\n",a[(n-1)/2]);
return 0;
}