顺序表基本运算算法(seqlist.cpp)
#include <stdio.h>
#define MAXL 100
typedef int KeyType;
typedef char InfoType;
typedef struct
{ KeyType key;
InfoType data;
} RecType;
void swap(RecType x,RecType y)
{
RecType tmp=x;
x=y; y=tmp;
}
void CreateList(RecType R[],KeyType keys[],int n)
{
for (int i=0;i<n;i++)
R[i].key=keys[i];
}
void DispList(RecType R[],int n)
{
for (int i=0;i<n;i++)
printf("%d ",R[i].key);
printf("\n");
}
void CreateList1(RecType R[],KeyType keys[],int n)
{
for (int i=1;i<=n;i++)
R[i].key=keys[i-1];
}
void DispList1(RecType R[],int n)
{
for (int i=1;i<=n;i++)
printf("%d ",R[i].key);
printf("\n");
}
直接插入排序算法
#include "seqlist.cpp"
void InsertSort(RecType R[],int n)
{ int i, j; RecType tmp;
for (i=1;i<n;i++)
{
if (R[i].key<R[i-1].key)
{
tmp=R[i];
j=i-1;
do
{
R[j+1]=R[j];
j--;
} while (j>=0 && R[j].key>tmp.key);
R[j+1]=tmp;
}
printf(" i=%d: ",i); DispList(R,n);
}
}
int main()
{
int n=10;
RecType R[MAXL];
KeyType a[]={9,8,7,6,5,4,3,2,1,0};
CreateList(R,a,n);
printf("排序前:"); DispList(R,n);
InsertSort(R,n);
printf("排序后:"); DispList(R,n);
return 1;
}
折半插入排序算法
#include "seqlist.cpp"
void BinInsertSort(RecType R[],int n)
{ int i, j, low, high, mid;
RecType tmp;
for (i=1;i<n;i++)
{
if (R[i].key<R[i-1].key)
{
tmp=R[i];
low=0; high=i-1;
while (low<=high)
{
mid=(low+high)/2;
if (tmp.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]=tmp;
}
printf(" i=%d: ",i);
DispList(R,n);
}
}
int main()
{
int n=10;
RecType R[MAXL];
KeyType a[]={9,8,7,6,5,4,3,2,1,0};
CreateList(R,a,n);
printf("排序前:"); DispList(R,n);
BinInsertSort(R,n);
printf("排序后:"); DispList(R,n);
return 1;
}
希尔排序算法
#include "seqlist.cpp"
void ShellSort(RecType R[],int n)
{ int i,j,d;
RecType tmp;
d=n/2;
while (d>0)
{ for (i=d;i<n;i++)
{ tmp=R[i];
j=i-d;
while (j>=0 && tmp.key<R[j].key)
{ R[j+d]=R[j];
j=j-d;
}
R[j+d]=tmp;
}
printf(" d=%d: ",d); DispList(R,n);
d=d/2;
}
}
int main()
{
int n=10;
RecType R[MAXL];
KeyType a[]={9,8,7,6,5,4,3,2,1,0};
CreateList(R,a,n);
printf("排序前:"); DispList(R,n);
ShellSort(R,n);
printf("排序后:"); DispList(R,n);
return 1;
}
冒泡排序算法
#include "seqlist.cpp"
void BubbleSort(RecType R[],int n)
{
int i,j,k;
RecType tmp;
for (i=0;i<n-1;i++)
{
for (j=n-1;j>i;j--)
if (R[j].key<R[j-1].key)
{
tmp=R[j];
R[j]=R[j-1];
R[j-1]=tmp;
}
printf(" i=%d: ",i);
DispList(R,n);
}
}
int main()
{
int n=10;
RecType R[MAXL];
KeyType a[]={9,8,7,6,5,4,3,2,1,0};
CreateList(R,a,n);
printf("排序前:"); DispList(R,n);
BubbleSort(R,n);
printf("排序后:"); DispList(R,n);
return 1;
}
改进的冒泡排序算法
#include "seqlist.cpp"
void BubbleSort1(RecType R[],int n)
{ int i,j;
bool exchange;
RecType tmp;
for (i=0;i<n-1;i++)
{ exchange=false;
for (j=n-1;j>i;j--)
if (R[j].key<R[j-1].key)
{ tmp=R[j];
R[j]=R[j-1];
R[j-1]=tmp;
exchange=true;
}
printf(" i=%d: ",i);
DispList(R,n);
if (!exchange)
return;
}
}
int main()
{
int n=10;
RecType R[MAXL];
KeyType a[]={0,1,7,2,5,4,3,6,8,9};
CreateList(R,a,n);
printf("排序前:"); DispList(R,n);
BubbleSort1(R,n);
printf("排序后:"); DispList(R,n);
return 1;
}
快速排序算法
#include "seqlist.cpp"
int count=0;
int partition(RecType R[],int s,int t)
{
int i=s,j=t;
RecType tmp=R[i];
while (i<j)
{ while (j>i && R[j].key>=tmp.key)
j--;
R[i]=R[j];
while (i<j && R[i].key<=tmp.key)
i++;
R[j]=R[i];
}
R[i]=tmp;
return i;
}
void QuickSort(RecType R[],int s,int t)
{ int i;
RecType tmp;
if (s<t)
{ count++;
i=partition(R,s,t);
DispList(R,10);
QuickSort(R,s,i-1);
QuickSort(R,i+1,t);
}
}
int main()
{
int i,n=10;
RecType R[MAXL];
KeyType a[]={6,8,7,9,0,1,3,2,4,5};
CreateList(R,a,n);
printf("排序前:"); DispList(R,n);
QuickSort(R,0,n-1);
printf("排序后:"); DispList(R,n);
printf("count=%d\n",count);
return 1;
}
简单选择排序算法
#include "seqlist.cpp"
void SelectSort(RecType R[],int n)
{
int i,j,k;
RecType temp;
for (i=0;i<n-1;i++)
{
k=i;
for (j=i+1;j<n;j++)
if (R[j].key<R[k].key)
k=j;
if (k!=i)
{
temp=R[i];
R[i]=R[k];
R[k]=temp;
}
printf(" i=%d: ",i); DispList(R,n);
}
}
int main()
{
int n=10;
RecType R[MAXL];
KeyType a[]={9,8,7,6,5,4,3,2,1,0};
CreateList(R,a,n);
printf("排序前:"); DispList(R,n);
SelectSort(R,n);
printf("排序后:"); DispList(R,n);
return 1;
}
堆排序算法
#include "seqlist.cpp"
void sift(RecType R[],int low,int high)
{
int i=low,j=2*i;
RecType temp=R[i];
while (j<=high)
{
if (j<high && R[j].key<R[j+1].key)
j++;
if (temp.key<R[j].key)
{
R[i]=R[j];
i=j;
j=2*i;
}
else break;
}
R[i]=temp;
}
void HeapSort(RecType R[],int n)
{
int i;
RecType tmp;
for (i=n/2;i>=1;i--)
sift(R,i,n);
printf("初始堆:"); DispList1(R,n);
for (i=n;i>=2;i--)
{ tmp=R[1];
R[1]=R[i];
R[i]=tmp;
printf("第%d趟: ",n-i+1); DispList1(R,n);
sift(R,1,i-1);
printf("筛选为:"); DispList1(R,n);
}
}
int main()
{
int n=10;
RecType R[MAXL];
KeyType a[]={15,18,29,12,35,32,27,23,10,20};
CreateList1(R,a,n);
printf("排序前:"); DispList1(R,n);
HeapSort(R,n);
printf("排序后:"); DispList1(R,n);
return 1;
}
最低位优先的基数排序算法
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAXE 20
#define MAXR 10
#define MAXD 8
typedef struct node
{
char data[MAXD];
struct node *next;
} NodeType;
void CreaLink(NodeType *&p,char *a[],int n);
void DispLink(NodeType *p);
void RadixSort(NodeType *&p,int r,int d)
{
NodeType *head[MAXR],*tail[MAXR],*t;
int i,j,k;
for (i=0;i<=d-1;i++)
{
for (j=0;j<r;j++)
head[j]=tail[j]=NULL;
while (p!=NULL)
{
k=p->data[i]-'0';
if (head[k]==NULL)
{
head[k]=p;
tail[k]=p;
}
else
{
tail[k]->next=p;
tail[k]=p;
}
p=p->next;
}
p=NULL;
for (j=0;j<r;j++)
if (head[j]!=NULL)
{
if (p==NULL)
{
p=head[j];
t=tail[j];
}
else
{
t->next=head[j];
t=tail[j];
}
}
t->next=NULL;
printf(" 按%s位排序\t",(i==0?"个":"十"));
DispLink(p);
}
}
void CreateLink(NodeType *&p,char a[MAXE][MAXD],int n)
{
int i;
NodeType *s,*t;
for (i=0;i<n;i++)
{
s=(NodeType *)malloc(sizeof(NodeType));
strcpy(s->data,a[i]);
if (i==0)
{
p=s;t=s;
}
else
{
t->next=s;t=s;
}
}
t->next=NULL;
}
void DispLink(NodeType *p)
{
while (p!=NULL)
{
printf("%c%c ",p->data[1],p->data[0]);
p=p->next;
}
printf("\n");
}
void main()
{
int n=10,r=10,d=2;
int i,j,k;
NodeType *p;
char a[MAXE][MAXD];
int b[]={75,23,98,44,57,12,29,64,38,82};
for (i=0;i<n;i++)
{
k=b[i];
for (j=0;j<d;j++)
{
a[i][j]=k%10+'0';
k=k/10;
}
a[i][j]='\0';
}
CreateLink(p,a,n);
printf("\n");
printf(" 初始关键字\t");
DispLink(p);
RadixSort(p,10,2);
printf(" 最终结果\t");
DispLink(p);
printf("\n");
}
自底向上的二路归并排序算法
#include <malloc.h>
#include "seqlist.cpp"
void Merge(RecType R[],int low,int mid,int high)
{
RecType *R1;
int i=low,j=mid+1,k=0;
R1=(RecType *)malloc((high-low+1)*sizeof(RecType));
while (i<=mid && j<=high)
if (R[i].key<=R[j].key)
{
R1[k]=R[i];
i++;k++;
}
else
{
R1[k]=R[j];
j++;k++;
}
while (i<=mid)
{
R1[k]=R[i];
i++;k++;
}
while (j<=high)
{
R1[k]=R[j];
j++;k++;
}
for (k=0,i=low;i<=high;k++,i++)
R[i]=R1[k];
}
void MergePass(RecType R[],int length,int n)
{
int i;
for (i=0;i+2*length-1<n;i=i+2*length)
Merge(R,i,i+length-1,i+2*length-1);
if (i+length-1<n-1)
Merge(R,i,i+length-1,n-1);
printf("length=%d: ",length);
DispList(R,n);
}
void MergeSort(RecType R[],int n)
{
int length;
for (length=1;length<n;length=2*length)
MergePass(R,length,n);
}
int main()
{
int n=10;
RecType R[MAXL];
KeyType a[]={9,8,7,6,5,4,3,2,1,0};
CreateList(R,a,n);
printf(" 排序前:"); DispList(R,n);
MergeSort(R,n);
printf(" 排序后:"); DispList(R,n);
return 1;
}
自顶向下的二路归并排序算法
#include <malloc.h>
#include "seqlist.cpp"
void Merge(RecType R[],int low,int mid,int high)
{
RecType *R1;
int i=low,j=mid+1,k=0;
R1=(RecType *)malloc((high-low+1)*sizeof(RecType));
while (i<=mid && j<=high)
if (R[i].key<=R[j].key)
{
R1[k]=R[i];
i++;k++;
}
else
{
R1[k]=R[j];
j++;k++;
}
while (i<=mid)
{
R1[k]=R[i];
i++;k++;
}
while (j<=high)
{
R1[k]=R[j];
j++;k++;
}
for (k=0,i=low;i<=high;k++,i++)
R[i]=R1[k];
}
void MergeSortDC(RecType R[],int low,int high)
{
int mid;
if (low<high)
{ mid=(low+high)/2;
MergeSortDC(R,low,mid);
MergeSortDC(R,mid+1,high);
Merge(R,low,mid,high);
}
}
void MergeSort1(RecType R[],int n)
{
MergeSortDC(R,0,n-1);
}
int main()
{
int n=10;
RecType R[MAXL];
KeyType a[]={9,8,7,6,5,4,3,2,1,0};
CreateList(R,a,n);
printf("排序前:"); DispList(R,n);
MergeSort1(R,n);
printf("排序后:"); DispList(R,n);
return 1;
}