直接插入排序
int straight_insert_sort(int *num,int n)
{
int i;
int j;
for(i=2;i<n;i++)
{
if(num[i]<num[i-1])
{
num[0]=num[i];
num[i]=num[i-1];
for(j=i-2;num[0]<num[j];j--)
{
num[j+1]=num[j];
}
num[j+1]=num[0];
}
}
return 0;
}
折半插入排序
int Binary_insert_sort(int *num, int n)
{
int mid,high,low=1;
int i=high,j;
for(i=2;i<n;i++)
{
low=1;
high=i-1;
num[0]=num[i];
while(low<=high)
{
mid=(low+high)/2;
if(num[mid]<num[0])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
for(j=i-1;j>high;j--)
{
num[j+1]=num[j];
}
num[j+1]=num[0];
}
冒泡排序
int bubble_sort(int *num,int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(num[j]>num[j+1])
{
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
return 0;
}
快速排序
int quick_sort(int *num,int low,int high)
{
int i=low,j=high,temp,poi;
if(low<high)
{
temp=num[low];
while(low<high)
{
while(high>low&&num[high]>=temp)
{
high--;
}
num[low]=num[high];
while(high>low&&num[low]<temp)
{
low++;
}
num[high]=num[low];
}
num[low]=temp;
quick_sort(num,i,low-1);
quick_sort(num,low+1,j);
}
return 0;
}
堆排序
int heapadjust(int *num,int start,int end)
{
int i;
int temp;
temp=num[start];
for(i=2*start+1;i<=end;i=i*2+1)
{
if(i<end&&num[i]<num[i+1])
{
i++;
}
if(temp>=num[i])
{
break;
}
num[start]=num[i];
start=i;
}
num[start]=temp;
return 0;
}
int heap_sort(int *num,int n)
{
int i,temp;
for(i=n/2-1;i>=0;i--)
{
heapadjust(num,i,n-1);
}
for(i=n-1;i>0;i--)
{
temp=num[0];
num[0]=num[i];
num[i]=temp;
heapadjust(num,0,i-1);
}
return 0;
}
归并排序
int merge(int num[],int temp[],int low,int high)
{
int mid=low+((high-low)>>1);
int pos=low;
int p1=low,p2=mid+1;
if(low<high)
{
merge(num,temp,low,mid);
merge(num,temp,mid+1,high);
while(p1<=mid&&p2<=high)
{
if(num[p1]<=num[p2])
{
temp[pos++]=num[p1++];
}
else
{
temp[pos++]=num[p2++];
}
}
while(p1<=mid)
{
temp[pos++]=num[p1++];
}
while(p2<=high)
{
temp[pos++]=num[p2++];
}
while(low<=high)
{
num[low]=temp[low];
low++;
}
}
return 0;
}
int merge_sort(int *num,int n)
{
int temp[n];
merge(num,temp,0,n-1);
return 0;
}
全部代码(选择对应函数可直接运行
#include<stdio.h>
int straight_insert_sort(int *num,int n)
{
int i;
int j;
for(i=2;i<n;i++)
{
if(num[i]<num[i-1])
{
num[0]=num[i];
num[i]=num[i-1];
for(j=i-2;num[0]<num[j];j--)
{
num[j+1]=num[j];
}
num[j+1]=num[0];
}
}
return 0;
}
int Binary_insert_sort(int *num, int n)
{
int mid,high,low=1;
int i=high,j;
for(i=2;i<n;i++)
{
low=1;
high=i-1;
num[0]=num[i];
while(low<=high)
{
mid=(low+high)/2;
if(num[mid]<num[0])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
for(j=i-1;j>high;j--)
{
num[j+1]=num[j];
}
num[j+1]=num[0];
}
}
int bubble_sort(int *num,int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(num[j]>num[j+1])
{
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
return 0;
}
int quick_sort(int *num,int low,int high)
{
int i=low,j=high,temp,poi;
if(low<high)
{
temp=num[low];
while(low<high)
{
while(high>low&&num[high]>=temp)
{
high--;
}
num[low]=num[high];
while(high>low&&num[low]<temp)
{
low++;
}
num[high]=num[low];
}
num[low]=temp;
quick_sort(num,i,low-1);
quick_sort(num,low+1,j);
}
return 0;
}
int heapadjust(int *num,int start,int end)
{
int i;
int temp;
temp=num[start];
for(i=2*start+1;i<=end;i=i*2+1)
{
if(i<end&&num[i]<num[i+1])
{
i++;
}
if(temp>=num[i])
{
break;
}
num[start]=num[i];
start=i;
}
num[start]=temp;
return 0;
}
int heap_sort(int *num,int n)
{
int i,temp;
for(i=n/2-1;i>=0;i--)
{
heapadjust(num,i,n-1);
}
for(i=n-1;i>0;i--)
{
temp=num[0];
num[0]=num[i];
num[i]=temp;
heapadjust(num,0,i-1);
}
return 0;
}
int merge(int num[],int temp[],int low,int high)
{
int mid=low+((high-low)>>1);
int pos=low;
int p1=low,p2=mid+1;
if(low<high)
{
merge(num,temp,low,mid);
merge(num,temp,mid+1,high);
while(p1<=mid&&p2<=high)
{
if(num[p1]<=num[p2])
{
temp[pos++]=num[p1++];
}
else
{
temp[pos++]=num[p2++];
}
}
while(p1<=mid)
{
temp[pos++]=num[p1++];
}
while(p2<=high)
{
temp[pos++]=num[p2++];
}
while(low<=high)
{
num[low]=temp[low];
low++;
}
}
return 0;
}
int merge_sort(int *num,int n)
{
int temp[n];
merge(num,temp,0,n-1);
return 0;
}
int main()
{
int num[]={17,7,6,8,9,77,2,5,4,1,15,52,67,35,25,6};
int n,i;
n=sizeof(num)/sizeof(int);
heap_sort(num,n);
for(i=0;i<n;i++)
{
printf("%d\n",num[i]);
}
return 0;
}