直接插入排序
#include <STDIO.H>
typedef int Keytype;
typedef struct
{
Keytype key;
int data;
}RecType;
void InsertSort(RecType R[],int n)
{
int i;
int j;
RecType temp;
for(i=1;i<n;i++)
{
temp=R[i];
j=i-1;
while (j>=0&&temp.key<R[j].key)
{
R[j+1]=R[j];
j--;
}
R[j+1]=temp;
}
}
二分插入排序:
void InsertSort1(RecType R[],int n)
{
int i,j,low,high,mid;
RecType temp;
for(i=1;i<n;i++)
{
temp=R[i];
low=0;
high=i-1;
while (low<=high)
{
mid=(high+low)/2;
if (temp.key<R[mid].key)
high=mid-1;
else
low=mid+1;
}
for (j=i-1;j>=high+1;j--)
R[j+1]=R[j];
R[high+1]=temp;
}
}
希尔排序:
void shellsort(Sqlist R[],int n)
{
int i,j,gap;
Sqlist temp;
gap=n/2;
while (gap>0)
{
for (i=gap;i<n;i++)
{
temp=R[i];
j=i-gap;
while (j>=0&&temp.data<R[j].data)
{
R[j+gap]=R[j];
j=j-gap;
}
R[j+gap]=temp;
}
gap=gap/2;
}
}
冒泡排序:
void Bubblesort(RecType R[],int n)
{
int i,j,exchang;
RecType temp;
for (i=0;i<n-1;i++)
{
exchang=0;
for (j=n-1;j>i;j--)
if (R[j].key<R[j-1].key)
{
temp=R[j].key;
R[j].key=R[j-1].key;
R[j-1].key=temp;
exchang=1;
}
if(exchang==0)
return ;
}
}