typedef struct
{
ElemType data;
KeyType key;
}
void countsort(listtype a[],listtype b[],int n)
{
int i,j,count;
for(i=0;i<=0;i++)
{
count=0;
for(j=0;j<=n;j++)
if(a[j].key<a[i].key)
count++;
b[count]=a[i];
}
}
void sort(int A[],int n)
{
int i,j,temp,minval,minidx;
for(i=0;i<n-1;i++)
{
minval=A[i];
minidx=i;
for(j=i+1;j<n;j++)
if(A[j]<minval)
{
minval=A[j];
minidx=j;
}
if(minidx!=i)
{
temp=A[i];
A[i]=A[minidx];
A[minidx]=temp;
}
}
}
#define Max 100
typedef struct stacknode
{
int left;
int right;
}Stack;
void quicksort(type a[],int left,int right)
{
Stack s[Max];
int top=-1;
top++;
s[top].left=left;
s[top].right=right;
while(top>-1)
{
left=s[top].left;
right=s[top].right;
top--;
while(left<right)
{
pivot=median3(a,left,right);
i=left;
j=right-1;
for(;;)
{
while(i<j&&a[i]<pivot)
i++;
while(i<j&&a[j]>pivot)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--
}
else break;
}
if(a[i]>pivot)
{
temp=a[i];
a[i]=a[right];
a[right]=temp;
}
top++;
s[top].left=i+1;
s[top].right=right;
right=i-1;
}
}
}
type median3(type a[],int left,int right)
{
int mid=(left+right)/2;
type temp;
if(a[left]>a[mid])
{
temp=a[left];
a[left]=a[mid];
a[mid]=temp;
}
if(a[a[right]>a[mid])
{
temp=a[right];
a[right]=a[mid];
a[mid]=temp;
}
if(a[right]<a[left])
{
temp=a[right];
a[right]=a[left];
a[left]=temp;
}
return a[right];
}
void insertsort(sqlist r,int n)//排序元素a[1]~a[n],直接插入排序
{
int i,j;
for(i=2;i<=n;i++)
{
r[0]=r[i];//r[0]是监视哨
j=i-1;
while(r[0].key<r[j].key)
{
r[j+1]=r[j];
j--;
}
r[j+1]=r[0];
}
}
void shellsort(sqlist r,int n)//shell排序
{
int i,j,gap;
struct rec temp;
gap=n/2;//增量置初值为n/2
while(gap>0)
{
for(i=gap+1;i<=n;i++)
{
j=i-gap;
while(j>0)
if(r[j].key>r[j+gap].key)
{
temp=a[j];
a[j]=a[j+gap];
a[j+gap]=temp;
j=j-gap;
}
else j=0;
}
gap=gap/2;
}
}
void shellsort(sqlist r,int n)
{
int i,j,gap;
struct rec temp;
gap=n/2;
while(gap>0)
{
for(i=gap+1;i<=n;i++)
{
j=i-gap;
while(j>0;)
if(r[j].key>r[j+gap].key)
{
temp=a[j];
a[j]=a[j+gap];
a[j+gap]=temp;
j=j-gap;
}
else j=0
}
gap=gap/2;
}
}
void bubblesort(sqlist r,int n)//冒泡排序,r[1]~r[n]
{
int i,j,exchange;
struct rec temp;
for(i=1;i<=n-1;i++)
{
exchange=0;
for(j=n;j>=i+1;j--)
if(r[j].key<r[j-1].key)
{
temp=a[j];
r[j]=r[j-1];
r[j-1]=temp;
exchange=1;
}
if(exchange==0)
return;
}
}
void quicksort(sqlist r,int s,int t)//快速排序r[s……t]
{
int i=s,j=t;
if(s<t)
{
r[0]=r[s];
do{
while(j>i&&r[j].key>=r[0].key)
j--;
if(i<j)
{
r[i]=r[j];
i++;
}
while(i<j&&r[i].key<=r[0].key)
i++;
if(i<j)
{
r[j]=a[i];
j--;
}
} while(i<j);
r[i]=r[0];
quicksort(r,s,j-1);
quiclsort(r,j+1,t);
}
}
12131
最新推荐文章于 2024-04-01 15:19:12 发布