排序算法总结

shell 排序算法总结

Latest Snippet Version: 1.01

/*
* Shell 排序算法在 1959 年由 D. Shell 发明。
* 也称为递减增量排序算法,各种实现在如何进行递减上有所不同。
* 不稳定,不需要辅助空间。
*/
/*
* Gonnet 算法,发表于 1991 年。
*/
int shellsortGo(int p[],int n)
{
int op=0;
int h,i,j,temp;
for(h=n; h>1; ) {
   h=(h<5)?1:(h*5-1)/11;
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*
* Incerpj-Sedgewick 算法,1985 年发表。
*/
int shellsortIS(int p[],int n)
{
int op=0;
int h,i,j,t,temp;
int incs[16] = { /* a1=3,a2=7,a3=16,a4=41,a5=101 */     
   1391376,   /* a1*a2*a3*a4*a5 */
   463792,   /* a2*a3*a4*a5 */
   198768,   /* a1*a3*a4*a5 */
   86961,   /* a1*a2*a4*a5 */
   33936,   /* a1*a2*a3*a5 */
   13776,   /* a1*a2*a3*a4 */
   4592,   /* a2*a3*a4 */
   1968,   /* a1*a3*a4 */
   861,   /* a1*a2*a4 */
   336,   /* a1*a2*a3 */
   112,   /* a2*a3 */
   48,    /* a1*a3 */
   21,    /* a1*a2 */
   7,   /* a2 */
   3,   /* a1 */
   1
};
for(t=0; t<16; t++) {
   h=incs[t];
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*
* Sedgewick 算法,1986 年发表。Knuth 推荐。
*/
int shellsortSe(int p[],int n)
{
int op=0;
int h,i,j,t,temp;
int incs[18] = {
   1045505, 587521, 260609, 146305, 64769,
   36289, 16001, 8929, 3905, 2161, 929,
   505, 209, 109, 41, 19, 5, 1
};
/* if (i%2) h[i]=8*pow(2,i)-6*pow(2,(i+1)/2)+1;
* else h[i]=9*pow(2,i)-9*pow(2,i/2)+1; */
    for (t=0; t<18; t++) {
   h=incs[t];
   if (h>n/3)
    continue;
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*
* Tokuda(徳田尚之)算法。发表于 1992 年。
*/
int shellsortTo(int p[],int n)
{
int op=0;
int h,i,j,t,temp;
int incs[18] = {
   1747331, 776591, 345152, 153401, 68178,
   30301, 13467, 5985, 2660, 1182, 525,
   233, 103, 46, 20, 9, 4, 1
};
/* h[i]=ceil((9*pow(2.25,i)-4)/5) */
    for (t=0; t<18; t++) {
   h=incs[t];
   if (h>n*4/9)
    continue;
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*
* Ciura 算法。发表于 2001 年。性能卓越。
*/
int shellsortCi(int p[],int n)
{
int op=0;
int h,i,j,t,temp;
int incs[18] = {
   2331004, 1036002, 460445, 204643, 90952,
   40423, 17965, 7985, 3549, 1577, 701,
   301, 132, 57, 23, 9, 4, 1
};
    for (t=0; t<18; t++) {
   h=incs[t];
   if (h>n*4/9)
    continue;
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*******************************************/
/* 下面几个算法有研究价值                  */
/*******************************************/
/*
* D. Shell 最初的算法。
*/
int shellsortSh(int p[],int n)
{
int op=0;
int h,i,j,temp;
for(h=n/2; h>0; h=h/2) {
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*
* Lazarus-Frank 算法,1960 年发表。
* 原为在必要时加 1 使所有增量都为奇数, 现修正为减 1。
*/
int shellsortLF(int p[],int n)
{
int op=0;
int h,i,j,temp;
for(h=n/2; h>0; h=h/2) {
   if (h%2==0)
    h--;
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*--------------------------------------*/
/*
* Hibbard 算法,1963 年发表。
* 1965 年 Papernov-Stasevich 给出了数学证明。
*/
int shellsortHb(int p[],int n)
{
int op=0;
int h,i,j,temp;
for(h=1; h<=n/4; h=h*2+1);
for( ; h>0; h=(h-1)/2) {
/* h = 1, 3, 7, 15, 31 ... 2^i-1 */
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*
* Papernov-Stasevich 算法, 1965? 年发表。
*/
int shellsortPS(int p[],int n)
{
int op=0;
int h,i,j,temp;
for(h=2; h<=n/4; h=h*2-1);
for( ; h>1; ) {
   h=(h==3)?1:(h+1)/2;
   /* h = 1, 3, 5, 9, 17, 33 ... 2^i+1 */
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*
* Knuth 算法,他建议在 n<1000 时使用。
*/
int shellsortKn(int p[],int n)
{
int op=0;
int h,i,j,temp;
for(h=1; h<=n/9; h=h*3+1);
for( ; h>0; h=h/3) {
/* h = 1, 4, 13, 40, 121, 364... 3*h+1 */
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*--------------------------------------*/
/*
* Pratt 算法,1971 年发表。
* 原为 h=2^p*3^q 现修正为 7^p*8^q。
*/
int shellsortPr(int p[],int n)
{
int op=0;
int h,i,j,t,temp;
int incs[28] = {
   262144, 229376, 200704, 175616, 153664, 134456,
   117649, 32768, 28672, 25088, 21952, 19208, 16807,
   4096, 3584, 3136, 2744, 2401, 512, 448, 392, 343,
   64, 56, 49, 8, 7, 1
};
for(t=0; t<28; t++) {
   h=incs[t];
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}
/*
* Sedgewick 算法, 1982 年发表。
*/
int shellsortSe82(int p[],int n)
{
int op=0;
int h,i,j,t,temp;
for (t=1; t*t<=n/4; t+=t);
    for (h=n/4; t>0; t/=2, h=t*t-(3*t)/2+1) {
/* h = 1, 8, 23, 77, 281, 1073, 4193, 16577,
* 65921, 262913, 1050113... 4^i+3*2^(i-1)+1 */
   for (i=h; i<n; i++) {
    temp=p[i];
    for (j=i-h; j>=0 && p[j]>temp; j-=h) {
     p[j+h]=p[j];
     op++;
    }
    p[j+h]=temp;
    op++;
   }
}
return op;
}

两分法查找例程
==============================================
Latest Snippet Version: 1.0


int binarysearch(int p[],int n,
   int value, /* 查找值 */
   int *m) /* 返回的是第一个大于等于查找值的元素
            * 位置和小于查找值的元素个数 */
{
int op=0;
int i=0; /* 头指针前面的元素小于查找值 */
int j=n-1; /* 尾指针和它后面的元素大于等于查找值 */
int k; /* 中间指针 */
if (n==0) { /* 空列表 */
   *m=0;
   return 0;
}
while (i<j) {
   k=(i+j)/2;
   if (p[k]<value)
    i=k+1;
   else
    j=k;
   op++;
}
/* 头尾指针指向同一个位置 */
if (p[i]>=value) /* 此位置上元素大于等于查找值 */
   *m=i;
else /* 全部元素都小于查找值 */
   *m=n;
op++;
return op;
}

简单的排序算法,冒泡,选择,插入,梳子
==============================================
Latest Snippet Version: 1.01


int selectsort(int p[], int n);
int insertsort(int p[],int n);
int bubblesort(int p[],int n);
int combsort(int p[], int n);

int bubblesort(int p[], int n)
{
int op=0;
int i, j;
int temp;
int flag=1;

for (j=n-1; flag>0 && j>0; j--) {
   flag=0;
   for (i=j; i>0; i--) {
    if (p[i-1]>p[i]) {
     temp=p[i];
     p[i]=p[i-1];
     p[i-1]=temp;
     flag=1;
    }
    op++;
   }
}
return op;
}

int selectsort(int p[], int n)
{
int op=0;
int i, j, max;
int temp;
for (j=n-1; j>0; j--) {
   max=j;
   temp=p[j];
   for(i=j-1; i>=0; i--) {
    if (p[i]>temp) {
     max=i;
     temp=p[i];
    }
    op++;
   }
   p[max]=p[j];
   p[j]=temp;
   op++;
}
return op;
}

int insertsort(int p[],int n)
{
int op=0;
int i,j,temp;
for (j=1; j<n; j++) {
   temp=p[j];
   for(i=j-1; i>=0 && p[i]>temp; i--) {
    p[i+1]=p[i];
    op++;
   }
   p[i+1]=temp;
   op++;
}
return op;
}

/*
* 改进的冒泡排序算法,性能接近堆排序。
* 在 1991 年由 S. Lacey 和 R. Box 发明。
* 据说在特定的重复性输入下有可能衰退成冒泡排序。
*/
int combsort(int p[], int n)
{
int op=0;
int i;
int temp;
int gap=n;
int flag=1;

while (gap>1 || flag!=0) {
   flag=0;
   gap=(gap==9||gap==10)?11:gap*10/13;
   if (gap==0)
    gap=1;
   for (i=n-1; i-gap>=0; i--) {
    if (p[i-gap]>p[i]) {
     temp=p[i];
     p[i]=p[i-gap];
     p[i-gap]=temp;
     flag=1;
    }
    op++;
   }
}
return op;
}

堆排序算法
====================================
Latest Snippet Version: 1.02


int heapsort(int p[],int n);

/*
* 堆排序算法在 1964 年由 J. Williams 发明。
* 不稳定,不需要辅助空间。
*/
int siftup(int p[],int i,int n);
int insert(int p[], int n);

int heapsort(int p[],int n)
{
int op=0;
int i,temp;

/* 自底向上建造堆 */
for (i=n/2-1; i>=0; i--)
   op+=siftup(p,i,n);

/* 自顶向下建造堆 */
/* for (i=2;i<=n;i++)
*   op+=insert(p,i); */


/* 交换堆顶与堆底的元素,筛选操作把目前处在堆顶的元素
* 插入到变小了堆中,同时从堆中选出新的最大元素到堆顶 */
for (i=n-1; i>0; i--) {
   temp=p[0];
   p[0]=p[i];
   p[i]=temp;
   op++;
   op+=siftup(p,0,i);

}
return op;
}
/*
* 筛选例程
*/
int siftup(int p[],
           int i, /* 堆顶的位置 */
           int n) /* 列表的长度 */
{
int op=0;
int j,temp;

temp=p[i]; /* 要插入的元素已经在根节点位置上,保存它的值 */
/* 要比较的节点位置是当前根节点的左子节,并且它在列表范围内 */
while ((j=i*2+1)<n) {
   op+=2;
   /* 要比较的左子节点有对应的右子节点,左子节点小于右子节点 */
   if (j+1<n && p[j]<p[j+1])
    j++; /* 要比较的节点位置是当前根节点的右子节点 */
   if (p[j]<=temp) /* 当前做比较的子节点的值小于等于要插入的值 */
    break; /* 停止下移 */
   p[i]=p[j]; /* 做比较的子节点的值上移到当前根节点 */
   i=j; /* 当前根节点下移到做比较的子节点的位置上 */
}
p[i]=temp; /* 插入要插入的值 */
op++;
return op;
}
/*
* 插入例程,把列表的最后一个元素插入到它前面的堆中
*/
int insert(int p[], int n)
{
int op=0;
int i=n-1,j;
int temp;
temp=p[i];
for (; i>0; i=j) {
   op++;
   j=(i-1)/2;
   if (p[j]>=temp)
    break;
   p[i]=p[j];
}
p[i]=temp;
op++;
return op;
}

就地归并排序算法的未做优化实现
==========================================
Latest Snippet Version: 1.03


int imergesort(int p[], int n);

extern int insertsort(int p[], int n);
extern int binarysearch(int p[],int n, int v, int *m);
static int exchange(int src[],int dest[],int n);
static int swapMergeSort(int p[], int swap[], int n, int flag);
static int swapMerge(int work[], int swap[], int m, int n,int flag);
static int replaceMerge(int p[],int m, int q, int n);
#define IN 1
#define OUT 0

/*
* in-place 归并排序算法的始作俑者和优化实现请参见:
* http://www.diku.dk/hjemmesider/ansatte/jyrki/Experimentarium/
* 不稳定,不需要辅助空间。余之实现意图说明标志性的方法而未做任何优化。
*/
int imergesort(int p[], int n)
{
int op=0;
int i;
int k=n; /* 在头部的未排序的元素数目 */
int m=0; /* 在尾部的已排序的元素数目 */

i=k/2;
/* 用列表的前半部分做交换空间,
* 对列表的后半部分做归并排序。*/
op+=swapMergeSort(&p[n-i],p,i,IN);
m+=i;
k-=i;
while(k>4) {
   i=k/2;
   /* 用未排序子列表的后半部分做交换空间,
   * 对未排序子列表的前半部分做归并排序*/
   op+=swapMergeSort(p,&p[i],i,IN);
   /* 把新排序出来的子列表与早先排序出来
   * 的子列表做不对称归并,它们之间的未
   * 排序空间被置换到列表头部。*/
   op+=replaceMerge(p,i,n-m,m);
   /* 列表的头部是未排序的,而尾部是已排序的 */
   m+=i;
   k-=i;
}
/* 最后的剩下的几个元素直接插入到已排序的列表中 */
op+=insertsort(p,n);
return op; /* 返回操作数 */
}

/*
* 前提:0 -> m-1 和 q -> q+n-1 是两个有序列表,
* 中间从 m -> q-1 是大小为 q-m 的未排序的空间。
* 要求 q>=2*m,即中间的未排序空间大于等于左面的列表。
* 结果:归并出从 q-m 开始的大小是 m+n 的有序列表,
* 0 到 q-m-1 是被置换出来的大小是 q-m 的未排序的空间。
*/
static int replaceMerge(int p[], /* 要归并的列表即左列表的头位置 */
        int m, /* 左列表的长度 */
        int q, /* 右列表的头位置 */
        int n) /* 右列表的长度 */
{
int op=0;
int i=0,j=0,t=0;
int w, r;
int *left=p;
int *right=&p[q];
int *dest=&p[q-m];

while (i<m && j<n) {
   if ((w=(n-j)/(m-i))==0)
    w=1;
   /* 把选择的左列表元素与右列表的 w 个元素中的最大值做比较 */
   if (left[i]>=right[j+w-1]) {
   /* 选择的左列表元素大于等于右列表的 m 个元素。*/
    op+=exchange(&right[j],&dest[t],w);
    t+=w;
    j+=w;
   } else {
    /* 以选择的左列表元素作为查找值在右列表的 w 个元素中找到小于
    * 查找值的元素个数 */
    op+=binarysearch(&right[j],w,left[i],&r);
    if (r!=0) {
     op+=exchange(&right[j],&dest[t],r);
     t+=r;
     j+=r;
    }
    op+=exchange(&left[i++],&dest[t++],1);
   }
}
if (i<m)
   op+=exchange(&left[i],&dest[t],m-i);

return op;
}
/*
* 交换过程,操作数量是 2*n+1 而不是 3*n,但不保持目标列表的
* 原有次序,故只能用在有序列表与无序列表之间的交换。
*/
static int exchange(int src[],int dest[],int n)
{
int i,temp;

if (n==0)
   return 0;
temp=dest[0];
for(i=0;i<n-1;i++) {
   dest[i]=src[i];
   src[i]=dest[i+1];
}
dest[i]=src[i];
src[i]=temp;

return 2*n+1;
}

static int swapMergeSort(int work[], int swap[], int n, int flag)
{
int op=0;
int temp;

if (n>1) {
   int m=n/2;
   op+=swapMergeSort(work,swap,m,flag^1);
   op+=swapMergeSort(work+m,swap+m,n-m,flag^1);
   op+=swapMerge(work,swap,m,n,flag);
}
else if (flag == OUT) {/* n==1 */
   temp=swap[0];
   swap[0]=work[0];
   work[0]=temp;
}
return op;
}

static int swapMerge(int work[], int swap[], int m, int n, int flag)
{
int *src, *dest;
int i=0, j=m, t=0;
int temp;

if (flag==OUT) {
   src="/work";
   dest=swap;
} else { /* flag==IN */
   src="/swap";
   dest=work;
}

temp=dest[t];
while (i<m && j<n)
   if (src[i] <= src[j]) {
    dest[t++] = src[i];
    src[i++] = dest[t];
   } else {
    dest[t++] = src[j];
    src[j++] = dest[t];
   }
while (i<m) {
   dest[t++] = src[i];
   if (t<n)
    src[i++] = dest[t];
   else
    src[i++] = temp;
}
while (j<n) {
   dest[t++] = src[j];
   if (t<n)
    src[j++] = dest[t];
   else
    src[j++] = temp;
}

return 2*n+1;
}

两路归并排序算法
========================================
Latest Snippet Version: 1.04


#i nclude <stdlib.h>
int mergesort(int p[], int n);
extern int insertsort(int p[], int n);
static int merge(int work[], int swap[], int m, int n, int flag);
int mergeSort(int p[], int n);
static int merge_sort(int p[], int swap[], int n, int flag);

/*
* 归并排序算法在 1938 年由 IBM 发明并在电动整理机上实现。
* 在 1945 年由 J. von Neumann 首次在 EDVAC 计算机上实现。
* 稳定,需要与序列同等大小的辅助空间。这里实现的是两路归并算法。
*/

#define IN 1
#define OUT 0
#define M 8 /* 启始路段长度 */

int mergesort(int p[], int n)
{
int op=0;
int * work=p;
int * swap;
int i,j,m;
int flag=OUT; /* 对换标志 */

if (n<=16)
   return insertsort(work,n);
swap=(int*)calloc(n,sizeof(int));
if (swap==NULL)
   return 0;

/* i 是经过插入排序的元素个数和未排序元素的开始位置 */
for(i=0;i+M<=n;i+=M)
   op+=insertsort(work+i,M);
if (i<n)
   op+=insertsort(work+i,n-i);
for(i=M; i<n; i<<=1,flag^=1) { /* i 为路段长度 */
   m=i<<1; /* m 为路段长度乘以归并的路数 */
   /* j 是已经归并路段的元素个数和未归并路段元素的开始位置 */
   for(j=0;j+m<=n;j+=m)
    op+=merge(work+j,swap+j,i,m,flag);
   if (j+i<n)
    op+=merge(work+j,swap+j,i,n-j,flag);
   else if (j<n)
    op+=merge(work+j,swap+j,n-j,n-j,flag);
}
if (flag==IN)
   op+=merge(work,swap,n,n,flag);

free(swap);
return op;
}

/*
* 两路归并过程。
*/
static int merge(int work[], /* 工作空间,就是要归并的列表 */
                 int swap[], /* 交换空间,不小于工作空间 */
                 int m, /* 前半部分列表长度和后半部分列表的开始位置 */
                 int n, /* 列表总长度 */
                 int flag) /* 换入换出标志 */
{
int *src, *dest;
int i=0, j=m, t=0;

if (flag==OUT) {
   src="/work";
   dest=swap;
} else { /* flag==IN */
   src="/swap";
   dest=work;
}

while (i<m && j<n)
   if (src[i] <= src[j])
    dest[t++] = src[i++];
   else
    dest[t++] = src[j++];

while (i<m)
   dest[t++] = src[i++];
while (j<n)
   dest[t++] = src[j++];

return n;
}

/**************************************/
/*   下面是递归原型实现,留做参考     */
/**************************************/
int mergeSort(int p[], int n)
{
int op;
int * temp;

temp=(int*)calloc(n,sizeof(int));
if (temp==NULL)
   return 0;
op=merge_sort(p,temp,n,IN);
free(temp);
return op;
}

static int merge_sort(int work[], int swap[], int n, int flag)
{
int op=0;

if (n>1) {
   int m=n/2;
   op+=merge_sort(work,swap,m,flag^1);
   op+=merge_sort(work+m,swap+m,n-m,flag^1);
   op+=merge(work,swap,m,n,flag);
}
else if (flag == OUT) /* n==1 */
   swap[0]=work[0];

return op;
}


快速排序算法
=============================================
Latest Snippet Version: 1.10


int quicksort(int p[],int n);
extern int insertsort(int p[], int n);
static int partition(int p[],int n,int *m);
int quickSort(int p[],int n);
static int quick_sort(int p[],int n);

/*
* 快速排序算法在 1962 年由 C. Hoare 发明。
* 不稳定,需要与 lg(n) 成比例的辅助空间。
*/
static struct stackframe { /* 栈帧 */
int * list;
int length;
};
static struct stackframe sp[64]; /* 栈指针 */
static unsigned int randx; /* 伪随机数 */
#define M 16

int quicksort(int p[],int n)
{
int op=0;
int i,k;
int *h,l;
int m; /* 基准值的位置 */
struct stackframe *fp; /* 帧指针*/
struct stackframe *stp; /* 栈顶指针 */

if (n<=16)
   return insertsort(p,n);
randx=p[0]%7875;

for (i=0,k=n; k>0; k>>=1,i++); /* i=[lg(n)] */
stp=sp+i;
fp=sp;
fp->list=p;
fp->length=n;
while (fp>=sp) {
   h=fp->list;
   l=fp->length;
   /* 采用 D. Musser 的限定划分深度的建议 */
   while (l>M && fp<=stp) {
    op+=partition(h,l,&m);
    fp->list=h+m+1;
    fp->length=l-m-1;
    fp++;
    l=m;
   }
   fp--;
}
op+=insertsort(p,n);
return op;
}

/*
* 基准值选择采用 C. Hoare 建议的随机选择策略。
*/
static int partition(int p[],int n,
                     int *m ) /* 返回的基准值的位置 */
{
int i=0; /* 头指针 */
int j=n-1; /* 尾指针 */
int pivot; /* 基准值 */
int k;
if (n<=1)
   return 0;
randx=(randx*421+1663)%7875; /* 线性同余伪随机数 */
k=randx%n;
/* 随机选择某个位置的元素作为基准值并保存它,
* 接着把头指针指向的元素复制到这个位置上 */
pivot=p[k];
p[k]=p[i];
/* p[i] 已被交换到 p[k],可以覆盖 */
while (i<j) { /* 头指针先于尾指针 */
   while (i<j && p[j]>=pivot) /* 尾指针指向的元素大于基准值 */
    j--; /* 前移尾指针 */
   if (i<j)
    p[i++]=p[j]; /* 替换当前p[i]内容为p[j]的内容, 后移头指针 */
    /* p[j] 已被交换可以覆盖 */
   while (i<j && p[i]<=pivot) /* 头指针指向的元素小于基准值 */
    i++; /* 后移头指针 */
   if (i<j)
    p[j--]=p[i]; /* 替换当前p[j]内容为p[i]的内容, 前移尾指针 */
    /* p[i] 已被交换可以覆盖 */
}
/* 如果最后一次交换的是 p[j],则 i 指针会移动成 i=j */
p[i]=pivot; /* 把保存的基准值保存到当前位置上 */
*m=i; /* 返回基准值当前的位置 */
return n;
}

/**************************************/
/*   下面是递归原型实现,留做参考     */
/**************************************/
int quickSort(int p[],int n)
{
if (n<=16)
   return insertsort(p,n);
randx=p[0]%7875;
return quick_sort(p,n);
}

static int quick_sort(int p[],int n)
{
int op=0;
int m;
   
if (n>1) {
   op+=partition(p,n,&m);
   op+=quick_sort(p,m);
   op+=quick_sort(p+m+1,n-m-1);
}
return op;
}

基数排序算法
=================================================
Latest Snippet Version: 1.0


#i nclude <stdlib.h>
int radixsort(int p[], int n);

int distribute(int *src, int *dest, int n, int idx);
/*
* 基数排序算法的最早书面记述在 1923 年由 IBM 发表。当时实
* 现在电动排序机上。在 1954 年由 H. Seward 在计算机上实现。
* 稳定,需要与序列同等大小的辅助空间。
*/
int radixsort(int p[], int n)
{
int * swap;

swap=(int *)calloc(n,sizeof(int));
if (swap==NULL)
   return 0;

/* 如果处理器不是小端字节序,而是大端字节序,
* 则下标应是 3,2,1,0 */
distribute(p, swap, n, 0);
distribute(swap, p, n, 1);
distribute(p, swap, n, 2);
distribute(swap, p, n, 3);

free(swap);
return 4*(2*n+512);
}

#define radix(x,y) (((unsigned char *)&(x))[(y)])
static int count[256];
/*
* 字节分布例程
*/
int distribute(int *src, int *dest, int n, int idx)
{
int i;
int index[256];
for (i=0; i<256; i++)
   count[i]=0;
/* 统计每个基数的元素数目 */
for (i=0; i<n; i++)
   count[radix(src[i],idx)]++;
/* 计算与每个基数相对应的堆的位置索引 */
for (index[0]=0, i=1; i<256; i++)
   index[i]=index[i-1]+count[i-1];
/* 把源列表中的元素分布到各个堆中 */
for (i=0; i<n; i++)
   dest[index[radix(src[i],idx)]++]=src[i];

return 2*n+512;
}

AVL 树数据结构
=================================================
Latest Snippet Version: 1.12


/* avl.h */
/*
* 高度平衡二叉树的一种重要方案。
* 在 1962 年由 G. Adelson-Velsky 和 E. Landis 发明。
*/
typedef int typekey;
typedef struct avlnode { /* 高度平衡树节点 */
typekey k; /* 键 */
char *v; /* 值 */
int bal;
/* 平衡因子,是节点的右子树的高度减取左子树的高度的高度差,
* 当这个高度差为 -1,0,1 的时候分别是偏左平衡,等高平衡,偏右平衡 */
struct avlnode *left, *right; /* 指向子树的指针 */
} node, *tree;
extern tree search(typekey, tree);
extern tree insert(typekey, tree);
extern tree delete(typekey, tree);
extern int height(tree);
extern int count(tree);
extern tree deltree(tree);
/* end of avl.h */

/*------------------------------------------------------------------------*/

/* avl.c */
#i nclude <stdlib.h>
#i nclude <stdio.h>
#i nclude "avl.h"

tree search(typekey, tree);
tree insert(typekey, tree);
tree delete(typekey, tree);
int height(tree);
int heightR(tree);
int count(tree);
tree deltree(tree);
static tree insertR(tree);
static tree deleteR(tree);
static tree deltreeR(tree);
static tree lrot(tree);
static tree rrot(tree);
static node* newNode(void);
static void freeNode(node*);
static void Error(void);

static int flag; /* 高度增减标志 */
static int key; /* 要插入或删除的键 */
static int avl_height = 0; /* avl 树的高度 */
static tree pool = NULL; /* 节点缓冲池 */
static pool_num = 0; /* 节点缓冲池中节点的当前数目 */
#define POOL_SIZE 1 /* 节点缓冲池中节点的最大数目 */

/*
* 二叉树查找例程
*/
tree search(typekey key, tree t)
{
while(t != NULL)
   if (t->k > key)
    t = t->left;
   else if (t->k < key)
    t = t->right;
   else
   return t;
return NULL;
}
/*
* 插入例程。
*/
tree insert(typekey k, tree t)
{
key = k;
t = insertR(t);
if (flag == 1)
   avl_height++;
return t;
}
tree insertR(tree t)
{
if (t == NULL){ /* 当前节点为空 */
   t = newNode(); /* 建立新节点 */
   t->k = key;
   t->left = NULL;
   t->right = NULL;
   t->bal = 0; /* 叶子节点等高平衡 */
   flag = 1; /* 高度增加 */
  return t; /* 无需调整平衡 */
}
else if (t->k < key){
      t->right = insertR(t->right);
      t->bal += flag; /* 右子树高度可能增加 */
}
else if (t->k > key){
      t->left = insertR(t->left);
      t->bal -= flag; /* 左子树高度可能增加 */
}
else { /* (t->k == key) 键已经存在 */
   Error();
   flag = 0;
}

if (flag == 0) /* 快速结束出口 */
  return t;
/* 左子树高于右子树两层: 需要右旋 */
if(t->bal < -1) {
   /* 当前节点的左子节点偏右平衡 */
   if(t->left->bal > 0)
    /* 使当前节点的左子节点偏左平衡 */    
    t->left = lrot(t->left);
   t = rrot(t);
   flag = 0; /* 本节点的高度不增加 */
}
/* 右子树高于左子树两层: 需要左旋 */
else if(t->bal > 1) {
   /* 当前节点的右子节点左平衡 */
   if(t->right->bal < 0)
    /* 使当前节点的右子节点偏右平衡 */
    t->right = rrot(t->right);
   t = lrot(t);
   flag = 0; /* 本节点的高度不增加 */
}
else if(t->bal == 0)/* 某个子树增加高度后导致等高平衡 */
   flag = 0; /* 本节点的高度不增加 */
else /* 某个子树高度增加了,破坏了等高平衡 */
   flag = 1; /* 本节点的高度增加 */
return t;
}
/*
* 删除例程, 在 1965 年由 C. C. Foster 发明。
*/
tree delete(typekey k, tree t)
{
key = k;
t = deleteR(t);
if (flag == 1)
   avl_height--;
return t;
}
tree deleteR(tree t)
{
tree p;
char * temp;

if(t == NULL) { /* 键未找到 */
   Error();
   flag = 0;
}
else if(t->k < key) {
   t->right = deleteR(t->right);
   t->bal -= flag; /* 右子树高度可能减少 */
}
else if (t->k > key) {
   t->left = deleteR(t->left);
   t->bal += flag; /* 左子树高度可能减少 */
}
else { /* (t->k == key)键找到 */
    /* 有一个子树为空 */
   if (t->left == NULL) {
    p = t;
    t = t->right;
    freeNode(p);
    flag = 1; /* 高度减少 */
   return t; /* 无需调整平衡 */
   }
   else if (t->right == NULL) {
    p = t;
    t = t->left;
    freeNode(p);
    flag = 1; /* 有高度变化 */
   return t; /* 无需调整平衡 */
   /* 没有一个子树为空 */
   } else {  
    if(t->bal<0) {
     /* 找到前驱节点 */
     p = t->left;
     while (p->right != NULL)
      p = p->right;
     t->k = p->k;
     temp = t->v;
     t->v = p->v;
     p->v = temp;
     key = p->k;
     t->left = deleteR(t->left);
     t->bal += flag; /* 左子树高度可能减少 */
    } else {
     /* 找到后继节点 */
     p = t->right;
     while (p->left != NULL)
      p = p->left;
     t->k = p->k;
     temp = t->v;
     t->v = p->v;
     p->v = temp;
     key = p->k;
     t->right = deleteR(t->right);
     t->bal -= flag; /* 右子树高度可能减少 */
    }
   }
}  

   if (flag == 0 ) /* 快速结束出口 */
  return t;
/* 左子树高于右子树两层: 需要右旋 */
if(t->bal < -1) {
   /* 当前节点的左子节点偏右平衡 */
   if(t->left->bal > 0) {
    /* 使当前节点的左子节点偏左平衡 */
    t->left = lrot(t->left);
    flag = 1;
   }
   else if (t->left->bal == 0)
    flag = 0;/* 本节点的高度不减少 */
      else
    flag = 1;/* 本节点的高度减少 */
   t = rrot(t);
}
/* 右子树高于左子树两层: 需要左旋 */
else if(t->bal > 1) {
   /* 当前节点的右子节点左平衡 */
   if(t->right->bal < 0) {
    /* 使当前节点的右子节点偏右平衡 */
    t->right = rrot(t->right);
    flag = 1;
   }
   else if (t->right->bal == 0)
    flag = 0;/* 本节点的高度不减少 */
      else
    flag = 1;/* 本节点的高度减少 */
   t = lrot(t);
}
else if (t->bal == 0) /* 某个子树减少高度后导致等高平衡 */
   flag = 1; /* 本节点的高度减少 */
else /* 某个子树高度减少了,破坏了等高平衡 */
   flag = 0; /* 本节点的高度不减少 */
return t;
}

/*
* 左旋例程
*            X              Y
*           / /            / /
*          A   Y    ==>   X   C
*             / /        / /
*            B   C      A   B
* 前提:X 偏右不平衡, Y 偏右或等高平衡。
*/
static tree lrot(tree t)
{
tree temp;
int x,y;

temp = t;
t = t->right;
temp->right = t->left;
t->left = temp;
x = temp->bal;
y = t->bal;
/* 旋转之前:
/* 假定 X 的平衡因子是 x, Y 的平衡因子是 y,
* 设 A 的高度为 h, 则 Y 的高度为 h+x
* 节点 B 高度为 h+x-1-max(y,0);
* 节点 C 的高度为 h+x-1+min(y,0);
* 旋转之后:
* 节点 X 的新平衡因子是 x-1-max(y,0);
* 节点 Y 的新平衡因子是 C-(max(A,B)+1) => min(C-A-1,C-B-1)
*     => min(x-2+min(y,0),y-1)
*/
temp->bal = x-1-max(y, 0);
t->bal = min(x-2+min(y, 0), y-1);
return t;
}
/*
* 右旋例程
*            X              Y
*           / /            / /
*          Y   C    ==>   A   X
*         / /                / /
*        A   B              B   C
* 前提:X 偏左不平衡,Y 偏左或等高平衡。
*/
static tree rrot(tree t)
{
tree temp;
int x,y;

temp = t;
t = t->left;
temp->left = t->right;
t->right = temp;
x = temp->bal;
y = t->bal;
/* 旋转之前:
/* 假定 X 的平衡因子是 x, 节点 Y 的平衡因子是 y,
* 设 C 的高度为 h, 则 Y 的高度为 h-x
* 节点 A 高度为 h-x-1-max(y,0);
* 节点 B 的高度为 h-x-1+min(y,0);
* 旋转之后:
* 节点 X 的新平衡因子是 x+1-min(y,0)
* 节点 Y 的新平衡因子是 max(B,C)+1-A => max(B-A+1,C-A+1)
*     => max(y+1,x+2+max(y,0))
*/
temp->bal = x+1-min(y, 0);
t->bal = max(x+2+max(y, 0), y+1);
return t;
}

static void Error(void)
{
printf("/nError: insert or delete key/n");
}
static node* newNode(void)
{
node* p;
if (pool != NULL) {
   p = pool;
   pool = pool->right;
   pool_num--;
}
else
   p=(node*)malloc(sizeof(node));
p->v=NULL;
return p;
}
static void freeNode(node * p)
{

if (p->v != NULL)
   free(p->v);
if (pool_num < POOL_SIZE) {
   p->right = pool;
   pool = p;
   pool_num++;
}
else
   free(p);
}
int height(tree t)
{
return avl_height;
}
int count(tree t)
{
if (t == NULL)
  return 0;
return 1+count(t->left)+count(t->right);
}
tree deltree(tree t)
{
avl_height = 0;
return deltreeR(t);
}
tree deltreeR(tree t)
{
if (t != NULL) {
   t->left = deltreeR(t->left);
   t->right = deltreeR(t->right);
   freeNode(t);
}
return NULL;
}
/* end of avl.c */

倾斜树数据结构
=======================================
Latest Snippet Version: 1.1


/* splay.h*/
/*
* 自调整二叉树的一种重要方案。
* 在 1985 年由 D. D. Sleator 和 R. E. Tarjan 发明。
*/
typedef int typekey;
typedef struct node { /* 二叉查找树节点 */
typekey k; /* 键 */
char *v; /* 值 */
struct node *left, *right; /* 指向子树的指针 */
} node, *tree;

/* 查找例程用宏实现,倾斜例程在要查找的键存在的时候把要找的键转移到根节点,
* 查找例程先进行倾斜操作然后判断根节点是否包含所要的键 */
#define search(key,t) ((t)=splay((key),(t),0), ((t)!=NULL&&(t)->k==(key))?(t):NULL)
extern tree splay(typekey, tree, int);
extern tree splayTD(typekey, tree);
extern tree insert(typekey, tree);
extern tree delete(typekey, tree);
extern int height(tree);
extern int count(tree);
extern tree deltree(tree);
/* end of splay.h */

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章