- 直接插入
- 折半插入
- 希尔排序
- 冒泡排序
- 快速排序
- 简单选择排序
- 堆排序
- 二路归并排序
直接插入
void InsertSort(int R[],int n){
int i,j,temp;
for(i=1;i<n;++i){
temp=R[i];
j=i-1;
while(j>=0&&temp<R[j])
{
R[j+1]=R[j];
--j;
}
R[j+1]=temp;
}
}
折半插入
void BinSort(int R[],int n){
int low,high,i,j,temp,mid;
for(i=1;i<n;++i){
low=0;
high=i-1;
temp=R[i];
while(low<=high){
mid=(low+high)/2;
if(R[mid]>temp)
high=mid-1;
else
low=mid+1;
}
for(j=i-1;j>=high+1;--j)
R[j+1]=R[j];
R[j+1]=temp;
}
}
希尔排序
void ShellSort(int R[],int n){
int i,j;
int gap;
int temp;
for(gap=n/2;gap>0;gap/=2){
for(i=gap;i<n;++i){
temp=R[i];
for(j=i-gap;j>=0&&temp<R[j];j-=gap)
R[j+gap]=R[j];
R[j+gap]=temp;
}
}
}
冒泡排序
void BubbleSort(int R[],int n){
int i,j,temp,int flag;
for(i=n-1;i>=1;--i){
flag=0;
for(j=1;j<=i;++j)
if(R[j-1]>R[j]){
temp=R[j-1];
R[j-1]=R[j];
R[j]=temp;
flag=1;
}
if(flag==0) return;
}
}
快速排序
void QuickSort(int R[],int low,int high){
int i=low,j=high;
int temp;
if(low<high)
{
temp=R[low];
while(i<j)
{
while(i<j&&temp<R[j]) --j;
if(i<j){
R[i]=R[j];
++i;
}
while(i<j&&temp>R[i]) ++i;
if(i<j){
R[j]=R[i];
--j;
}
}
R[i]=temp;
QuickSort(R,low,i-1);
QuickSort(R,i+1,high);
}
}
简单选择排序
void SelectSort(int R[],int n){
int i,j,k,temp;
for(i=0;i<n;++i){
k=i;
for(j=i+1;j<n;++j)
if(R[k]>R[j])
k=j;
temp=R[k];
R[k]=R[i];
R[i]=temp;
}
}
堆排序
void Sift(int R[],int low,int high){
int i=low,j=2*i;
int temp=R[i];
while(j<=high){
if(j<high&&R[j]<R[j+1])
j++;
if(temp<R[j])
{
R[i]=R[j];
i=j;
j=2*i;
}
else
break;
}
R[i]=temp;
}
void heapSort(int R[],int n){
int i,temp;
for(i=n/2;i>=1;--i)
Sift(R,i,n);
for(i=n;i>=2;--i){
temp=R[1];
R[1]=R[i];
R[i]=temp;
Sift(R,1,i-1);
}
}