#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<time.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define overflow -2
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)<(b))
#define LQ(a,b) ((a)<=(b))
typedef int KeyType;
#define maxsize 20
typedef struct {
KeyType key;
}RedType;
typedef struct {
RedType *r;
int length;
}SqList;
void InitList_Sq(SqList &L)
{
L.r=(RedType *)malloc((maxsize+1)*sizeof(RedType));
if(!L.r) exit(overflow);
L.length=0;
}
int search_Seq(SqList L,KeyType key)
{
int i;
L.r[0].key=key;
for(i=L.length;!EQ(L.r[i].key,key);--i);
return i;
}
int search_Bin(SqList L,KeyType key)
{
int low,high,mid;
low=1;high=L.length;
while(low<=high)
{
mid=(low+high)/2;
if(EQ(key,L.r[mid].key)) return mid;
else if(LT(key,L.r[mid].key)) high=mid-1;
else low=mid+1;
}
return 0;
}
void InsertSort(SqList &L)
{
int i,j;
for(i=2;i<=L.length;++i)
if(LT(L.r[i].key,L.r[i-1].key))
{
L.r[0].key=L.r[i].key;
for(j=i-1;LT(L.r[0].key,L.r[j].key);--j)
L.r[j+1].key=L.r[j].key;
L.r[j+1].key=L.r[0].key;
}
}
void BubbleSort(SqList &L)
{
int i,j;
KeyType temp;
for(i=L.length-1;i>=1;--i)
for(j=1;j<=i;j++)
if(LT(L.r[j+1].key,L.r[j].key))
{
temp=L.r[j+1].key;
L.r[j+1].key=L.r[j].key;
L.r[j].key=temp;
}
}
int partition(SqList &L,int low,int high)
{
KeyType pivotkey;
L.r[0].key=L.r[low].key;
pivotkey=L.r[low].key;
while(low<high)
{
while(low<high && L.r[high].key>=pivotkey) --high;
L.r[low].key=L.r[high].key;
while(low<high && L.r[low].key<=pivotkey) ++low;
L.r[high].key=L.r[low].key;
}
L.r[low].key=L.r[0].key;
return low;
}
void QSort(SqList &L,int low,int high)
{
int pivotloc;
if(low<high)
{
pivotloc=partition(L,low,high);
QSort(L,low,pivotloc-1);
QSort(L,pivotloc+1,high);
}
}
void QuickSort(SqList &L)
{
QSort(L,1,L.length);
}
void SelectSort(SqList &L)
{
int i,j,k;
KeyType temp;
for(i=1;i<L.length;++i)
{
j=i;
for(k=i+1;k<=L.length;++k)
{
if(LT(L.r[k].key,L.r[j].key))
j=k;
}
if(i!=j)
{
temp=L.r[i].key;
L.r[i].key=L.r[j].key;
L.r[j].key=temp;
}
}
}
void ListGenRand(SqList &L,int n,unsigned k)
{
int i;
L.length=0;
srand((unsigned)time(0)+k);
for(i=1;i<=n;i++)
{
L.r[i].key=rand()%100;
++L.length;
}
}
void DispList(SqList L)
{
int i;
for(i=1;i<=L.length;i++)
printf("%2d ",L.r[i].key);
printf("\n");
}
int main()
{
SqList L;
int n,num;
unsigned k=0;
KeyType key;
InitList_Sq(L);
srand((unsigned)time(0));
n=!+rand()%maxsize;
printf("随机产生元素个数为:%d\n",n);
ListGenRand(L,n,++k);
printf(" 顺序查找表:");
DispList(L);
printf(" 请输入待查找的关键字值:");
scanf("%d",&key);
printf("顺序查找表结果:");
num=search_Seq(L,key);
if(num) printf("查找成功!第%d个元素。\n",num);
else printf("查找不成功!\n");
SelectSort(L);
printf(" 折半查找表:");
DispList(L);
printf("折半查找结果:");
num=search_Bin(L,key);
if(num)printf("查找成功!第%d个元素。\n",num);
else printf("查找不成功!\n");
printf("\n");
printf("插入排序结果:\n");
ListGenRand(L,n,++k);
printf(" 排序前:");
DispList(L);
InsertSort(L);
printf(" 排序后:");
DispList(L);
printf("冒泡排序结果:\n");
ListGenRand(L,n,++k);
printf(" 排序前:");
DispList(L);
BubbleSort(L);
printf(" 排序后:");
DispList(L);
printf("快速排序结果:\n");
ListGenRand(L,n,++k);
printf(" 排序前:");
DispList(L);
QuickSort(L);
printf(" 排序后:");
DispList(L);
printf("选择排序结果:\n");
ListGenRand(L,n,++k);
printf(" 排序前:");
DispList(L);
SelectSort(L);
printf(" 排序后:");
DispList(L);
return 0;
}