7.2 插入排序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int ElemType;
typedef struct{
ElemType *elem;
int TableLen;
}SSTable;
void ST_Init(SSTable &ST,int len)
{
ST.TableLen=len+1;
ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
int i;
srand(time(NULL));
for(i=0;i<ST.TableLen;i++)
{
ST.elem[i]=rand()%100;
}
}
void ST_print(SSTable ST)
{
for(int i=0;i<ST.TableLen;i++)
{
printf("%3d",ST.elem[i]);
}
printf("\n");
}
void InsertSort(ElemType A[],int n)
{
int i,j;
for(i=2;i<=n;i++)
{
if(A[i]<A[i-1])
{
A[0]=A[i];
for(j=i-1;A[0]<A[j];--j)
A[j+1]=A[j];
A[j+1]=A[0];
}
}
}
void MidInsertSort(ElemType A[],int n)
{
int i,j,low,high,mid;
for(i=2;i<=n;i++)
{
A[0]=A[i];
low=1;high=i-1;
while(low<=high)
{
mid=(low+high)/2;
if(A[mid]>A[0])
high=mid-1;
else
low=mid+1;
}
for(j=i-1;j>=high+1;--j)
A[j+1]=A[j];
A[high+1]=A[0];
}
}
void ShellSort(ElemType A[],int n)
{
int dk,i,j;
for(dk=n/2;dk>=1;dk=dk/2)
{
for(i=dk+1;i<=n;++i)
{
if(A[i]<A[i-dk])
{
A[0]=A[i];
for(j=i-dk;j>0&&A[0]<A[j];j=j-dk)
A[j+dk]=A[j];
A[j+dk]=A[0];
}
}
}
}
int main()
{
SSTable ST;
ST_Init(ST,10);
ST_print(ST);
InsertSort(ST.elem,10);
ST_print(ST);
system("pause");
}
7.3 交换排序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef int ElemType;
typedef struct{
ElemType *elem;
int TableLen;
}SSTable;
void ST_Init(SSTable &ST,int len)
{
ST.TableLen=len;
ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
int i;
srand(time(NULL));
for(i=0;i<ST.TableLen;i++)
{
ST.elem[i]=rand()%100;
}
}
void ST_print(SSTable ST)
{
for(int i=0;i<ST.TableLen;i++)
{
printf("%3d",ST.elem[i]);
}
printf("\n");
}
void swap(ElemType &a,ElemType &b)
{
ElemType tmp;
tmp=a;
a=b;
b=tmp;
}
void BubbleSort(ElemType A[],int n)
{
int i,j;
bool flag;
for(i=0;i<n-1;i++)
{
flag=false;
for(j=n-1;j>i;j--)
{
if(A[j-1]>A[j])
{
swap(A[j-1],A[j]);
flag=true;
}
}
if(false==flag)
return;
}
}
int Partition(int* arr, int left, int right)
{
int k, i;
for (i = left, k = left; i < right; i++)
{
if (arr[i] < arr[right])
{
swap(arr[k], arr[i]);
k++;
}
}
swap(arr[k], arr[right]);
return k;
}
void QuickSort(ElemType A[],int low,int high)
{
if(low<high)
{
int pivotpos=Partition(A,low,high);
QuickSort(A,low,pivotpos-1);
QuickSort(A,pivotpos+1,high);
}
}
int main()
{
SSTable ST;
ElemType A[10]={ 64, 94, 95, 79, 69, 84, 18, 22, 12 ,78};
ST_Init(ST,10);
memcpy(ST.elem,A,sizeof(A));
ST_print(ST);
QuickSort(ST.elem,0,9);
ST_print(ST);
system("pause");
}
7.4 选择排序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef int ElemType;
typedef struct{
ElemType *elem;
int TableLen;
}SSTable;
void ST_Init(SSTable &ST,int len)
{
ST.TableLen=len;
ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
int i;
srand(time(NULL));
for(i=0;i<ST.TableLen;i++)
{
ST.elem[i]=rand()%100;
}
}
void ST_print(SSTable ST)
{
for(int i=0;i<ST.TableLen;i++)
{
printf("%3d",ST.elem[i]);
}
printf("\n");
}
void swap(ElemType &a,ElemType &b)
{
ElemType tmp;
tmp=a;
a=b;
b=tmp;
}
void SelectSort(ElemType A[],int n)
{
int i,j,min;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(A[j]<A[min])
min=j;
}
if(min!=i)
{
swap(A[i],A[min]);
}
}
}
void AdjustDown(ElemType A[],int k,int len)
{
int i;
A[0]=A[k];
for(i=2*k;i<=len;i*=2)
{
if(i<len&&A[i]<A[i+1])
i++;
if(A[0]>=A[i])
break;
else{
A[k]=A[i];
k=i;
}
}
A[k]=A[0];
}
void BuildMaxHeap(ElemType A[],int len)
{
for(int i=len/2;i>0;i--)
{
AdjustDown(A,i,len);
}
}
void HeapSort(ElemType A[],int len)
{
int i;
BuildMaxHeap(A,len);
for(i=len;i>1;i--)
{
swap(A[i],A[1]);
AdjustDown(A,1,i-1);
}
}
int main()
{
SSTable ST;
ElemType A[10]={ 64, 94, 95, 79, 69, 84, 18, 22, 12 ,99};
ST_Init(ST,10);
memcpy(ST.elem,A,sizeof(A));
ST_print(ST);
HeapSort(ST.elem,9);
ST_print(ST);
system("pause");
}
7.5 归并排序
#include <stdio.h>
#include <stdlib.h>
#define N 7
typedef int ElemType;
void Merge(ElemType A[],int low,int mid,int high)
{
ElemType B[N];
int i,j,k;
for(k=low;k<=high;k++)
B[k]=A[k];
for(i=low,j=mid+1,k=i;i<=mid&&j<=high;k++)
{
if(B[i]<=B[j])
A[k]=B[i++];
else
A[k]=B[j++];
}
while(i<=mid)
A[k++]=B[i++];
while(j<=high)
A[k++]=B[j++];
}
void MergeSort(ElemType A[],int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
MergeSort(A,low,mid);
MergeSort(A,mid+1,high);
Merge(A,low,mid,high);
}
}
void print(int* a)
{
for(int i=0;i<N;i++)
{
printf("%3d",a[i]);
}
printf("\n");
}
int main()
{
int A[7]={49,38,65,97,76,13,27};
MergeSort(A,0,6);
print(A);
system("pause");
}