//冒泡排序
void bubblesort(int arr[],int len)
{
if(NULL==arr||len<=0)return;
int i,j,t;
for(i=0; i<len-1; ++i)
for(j=0; j<len-1-i; ++j)
if(arr[j]>arr[j+1])
(t=arr[j],arr[j]=arr[j+1],arr[j+1]=t);
}
//插入排序
void insertsort(int arr[],int len)
{
if(<span style="font-family: Arial, Helvetica, sans-serif;">NULL==arr</span><span style="font-family: Arial, Helvetica, sans-serif;">||len<=0)return;</span>
int i,j,key;
for(i=1; i<len; i++)
{
key = arr[i];
for(j=i-1; j>=0; --j)
if(arr[j]>key)
arr[j+1] = arr[j];
else
break;
arr[j+1] = key;
}
}
//选择排序
void selectsort(int arr[],int len)
{
int i,j,_min,t;
for(i=0; i<len-1; ++i)
{
_min = i;
for(j=i+1; j<len; ++j)
if(arr[_min]>arr[j])_min=j;
if(_min!=i)
(t=arr[_min],arr[_min]=arr[i],arr[i]=t);
}
}
//shell排序(增量排序)
void shellsort(int arr[],int len)
{
int i,j,key,dk;
dk = len/2;
while(dk)
{
for(i=dk; i<len; ++i)
{
key = arr[i];
for(j=i-dk; j>=0; j-=dk)
if(key<arr[j])
arr[j+dk] = arr[j];
else
break;
arr[j+dk] = key;
}
dk/=2;
}
}
//归并排序
void _merge(int*,int*,int,int,int);
void mergesort(int source[],int dest[],int s,int t)
{
int temp[t+1];//int temp[MAX];
if(s==t)
dest[s] = source[s];
else
{
int m = (s+t)/2;
mergesort(source,temp,s,m);
mergesort(source,temp,m+1,t);
_merge(temp,dest,s,m,t);//将temp[s...m]and temp[m+1...t]排好序存入dest
}
}
void _merge(int source[],int dest[],int s,int m,int t)
{
int i=s,j=m+1,k=s;
while(i<=m&&j<=t)
{
if(source[i]<source[j])
dest[k] = source[i++];
else
dest[k] = source[j++];
k++;
}
while(i<=m)
dest[k++] = source[i++];
while(j<=t)
dest[k++] = source[j++];
}
//快速排序
int part(int*,int,int);
void quicksort(int arr[],int s,int t)
{
if(s<t)
{
int p = part(arr,s,t);//返回key的位置key左边都小于key,右边都大于key
quicksort(arr,s,p);
quicksort(arr,p+1,t);
}
}
int part(int arr[],int s,int t)
{
int key = arr[s];//选取key
while(s<t)
{
while(s<t&&key<=arr[t])
--t;
arr[s] = arr[t];//小于key的放到左边
while(s<t&&key>=arr[s])
++s;
arr[t] = arr[s];//大于key放到右边
}
arr[s] = key;
return s;
}
//堆排序
void heapaddjust(int arr[],int start,int len);
void heapsort(int arr[],int len)
{
int i,t;
for(i=len/2; i>=0; --i)
heapaddjust(arr,i,len);
for(i=len; i>=0; --i)
{
heapaddjust(arr,0,i);
(t=arr[0],arr[0]=arr[i],arr[i]=t);//交换根和尾部节点
}
}
void heapaddjust(int arr[],int start,int len)
{
int key = arr[start],j;
while((start*2+1)<=len)
{
j = start*2+1;
if(j<len&&arr[j]<arr[j+1])//判断左节点和右节点那个大
++j;
if(arr[j]<=key)//下滤
break;
arr[start] = arr[j];
start = j;
j = j*2;
}
arr[start] = key;
}
以上代码经过gcc测试vc编译器可能需要修改
另外看到一个不错的的帖子带动画很容易理解
http://www.cricode.com/3212.html