[code="c++"]#include <iostream>
using namespace std;
#define SWAP(i,j) {int t=(i);(i)=(j);(j)=t;}
//插入排序
void InsertSort(int*a,int len)
{
for (int i=1;i<len;i++)
{
int j=i,x=a[i];
while (j && a[j-1]>x)a[j]=a[j-1],j--;
a[j]=x;
}
}
//选择排序
void SelectSort(int*a,int len)
{
for (int i=1,j,k;i<len;i++)
{
for (j=i,k=i-1;j<len;j++)
if (a[j]<a[k])k=j;
if (k>=i)SWAP(a[i-1],a[k]);
}
}
//冒泡排序
void BubbletSort(int*a,int len)
{
for (bool bSwap=true;bSwap;len--)
{
bSwap=false;
for (int j=1;j<len;j++)
if (a[j-1]>a[j]){SWAP(a[j-1],a[j]);bSwap=true;}
}
}
//堆调整
void HeapAdjust(int *a,int root,int len)
{
int child,x=a;
while (child=root<<1|1,child<len)
{
if (child<len-1 && a[child]<a[child+1])child++;
if (x<a[child]){a=a[child];root=child;}
else break;
}
a=x;
}
//堆排序
void HeapSort(int*a,int len)
{
for (int i=len/2-1;i>=0;i--)
HeapAdjust(a,i,len);
while (--len)
{
SWAP(a[0],a[len]);
HeapAdjust(a,0,len);
}
}
//归并
void Merge(int*a,int len1,int len2)
{
int *a1=new int[len1+1],*a2=new int[len2+1],len=len1+len2;
for (int i=0;i<len1;i++)
a1[i]=a[i];
for (int i=0;i<len2;i++)
a2[i]=a[len1+i];
a1[len1]=a2[len2]=INT_MAX;
for (int i=0,j=0,k=0;k<len;k++)
if (a1[i]<a2[j])a[k]=a1[i++];
else a[k]=a2[j++];
delete[] a1;delete[] a2;
}
//归并排序
void MergeSort(int*a,int len)
{
if (len>1)
{
int c=len/2;
MergeSort(a,c);
MergeSort(a+c,len-c);
Merge(a,c,len-c);
}
}
//划分
int Partition(int*a,int len)
{
int x=a[--len],i=-1;
for (int j=0;j<len;j++)
if (a[j]<x){i++;SWAP(a[i],a[j]);}
SWAP(a[i+1],a[len]);
return i+1;
}
//快速排序
void QuickSort(int*a,int len)
{
if (len > 0)
{
int q=Partition(a,len);
if (q<len-q)
{
QuickSort(a,q-1);
QuickSort(a+q+1,len-q-1);
}
else
{
QuickSort(a+q+1,len-q-1);
QuickSort(a,q-1);
}
}
}
//希尔插入
void ShellInsert(int*a,int inc,int len)
{
for (int i=inc;i<len;i+=inc)
{
int j=i,x=a[i];
while (j>0 && a[j-inc]>x)a[j]=a[j-inc],j-=inc;
a[j]=x;
}
}
//插入式希尔排序
void ShellSort(int*a,int len)
{
int inc=len;
do
{
inc=inc/3+1;
for(int s=0;s<inc;s++)
ShellInsert(a-s,inc,len+s);
}
while (inc>1);
}[/code]
using namespace std;
#define SWAP(i,j) {int t=(i);(i)=(j);(j)=t;}
//插入排序
void InsertSort(int*a,int len)
{
for (int i=1;i<len;i++)
{
int j=i,x=a[i];
while (j && a[j-1]>x)a[j]=a[j-1],j--;
a[j]=x;
}
}
//选择排序
void SelectSort(int*a,int len)
{
for (int i=1,j,k;i<len;i++)
{
for (j=i,k=i-1;j<len;j++)
if (a[j]<a[k])k=j;
if (k>=i)SWAP(a[i-1],a[k]);
}
}
//冒泡排序
void BubbletSort(int*a,int len)
{
for (bool bSwap=true;bSwap;len--)
{
bSwap=false;
for (int j=1;j<len;j++)
if (a[j-1]>a[j]){SWAP(a[j-1],a[j]);bSwap=true;}
}
}
//堆调整
void HeapAdjust(int *a,int root,int len)
{
int child,x=a;
while (child=root<<1|1,child<len)
{
if (child<len-1 && a[child]<a[child+1])child++;
if (x<a[child]){a=a[child];root=child;}
else break;
}
a=x;
}
//堆排序
void HeapSort(int*a,int len)
{
for (int i=len/2-1;i>=0;i--)
HeapAdjust(a,i,len);
while (--len)
{
SWAP(a[0],a[len]);
HeapAdjust(a,0,len);
}
}
//归并
void Merge(int*a,int len1,int len2)
{
int *a1=new int[len1+1],*a2=new int[len2+1],len=len1+len2;
for (int i=0;i<len1;i++)
a1[i]=a[i];
for (int i=0;i<len2;i++)
a2[i]=a[len1+i];
a1[len1]=a2[len2]=INT_MAX;
for (int i=0,j=0,k=0;k<len;k++)
if (a1[i]<a2[j])a[k]=a1[i++];
else a[k]=a2[j++];
delete[] a1;delete[] a2;
}
//归并排序
void MergeSort(int*a,int len)
{
if (len>1)
{
int c=len/2;
MergeSort(a,c);
MergeSort(a+c,len-c);
Merge(a,c,len-c);
}
}
//划分
int Partition(int*a,int len)
{
int x=a[--len],i=-1;
for (int j=0;j<len;j++)
if (a[j]<x){i++;SWAP(a[i],a[j]);}
SWAP(a[i+1],a[len]);
return i+1;
}
//快速排序
void QuickSort(int*a,int len)
{
if (len > 0)
{
int q=Partition(a,len);
if (q<len-q)
{
QuickSort(a,q-1);
QuickSort(a+q+1,len-q-1);
}
else
{
QuickSort(a+q+1,len-q-1);
QuickSort(a,q-1);
}
}
}
//希尔插入
void ShellInsert(int*a,int inc,int len)
{
for (int i=inc;i<len;i+=inc)
{
int j=i,x=a[i];
while (j>0 && a[j-inc]>x)a[j]=a[j-inc],j-=inc;
a[j]=x;
}
}
//插入式希尔排序
void ShellSort(int*a,int len)
{
int inc=len;
do
{
inc=inc/3+1;
for(int s=0;s<inc;s++)
ShellInsert(a-s,inc,len+s);
}
while (inc>1);
}[/code]