八种排序算法(C语言简易版)

来源:互联网 发布:入骨相思知不知 编辑:程序博客网 时间:2024/06/10 09:19

排序,从小到大接触最多的事件,班级排名,学校排名
在程序中,排序的思路很多,优先列出如下算法

#include<stdio.h>#include<stdlib.h>//宏定义 MAXD暂定100 #define MAXD 100//定义关键字 类型为int typedef int KeyType;//其他数据项,类型暂定int typedef int InfoType;//元素类型 typedef struct{    //关键字项     KeyType key;    //其他数据项,类型为 InfoType    InfoType data; }RecType;//排序的元素类型定义 //基数排序//data域存放关键字,它是一个字符数组,//data[0..MAXD-1]依次存放关键字的低位到高位的各数字字符, // 关键字的实际位数由参数d指定。 typedef struct node{    char data[MAXD];    struct node *next;}RecType1;//直接插入排序算法//对R[0...n-1]按递增有序进行直接插入排序 void InsertSort(RecType R[],int n)  {    int i,j;    RecType tmp;    for(i=1;i<n;i++)    {        tmp=R[i];        j=i-1;        while(j>=0&&tmp.key<R[j].key)        {            R[j+1]=R[j];            j--;        }        R[j+1]=tmp;    }   }//希尔排序//对R[0...n-1]按递增有序进行希尔排序 void ShellSort(RecType R[],int n)  {    int i,j,gap;    RecType tmp;    gap=n/2;    while(gap>0)    {        for(i=gap;i<n;i++)        {            tmp=R[i];            j=i-gap;            while(j>=0&&tmp.key<R[j].key)            {                R[j+gap]=R[j];                j=j-gap;            }        }        gap=gap/2;    }} //交换排序 (冒泡排序) //对R[0...n-1]按递增有序进行交换排序 void BubbleSort(RecType R[],int n){    int i,j;    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;            }    } } //快速排序 //对R[s]至R[t] 按递增有序进行交换排序void QuickSort(RecType R[],int s,int t)   {    int i=s,j=t;    RecType tmp;    if(s<t)    {        tmp=R[s];        while(i!=j)        {            while(j>i&&R[j].key>=tmp.key)            {                j--;            }            while(i<j&&R[i].key<=tmp.key)            {                i++;            }        }        R[i]=tmp;        QuickSort(R,s,i-1);        QuickSort(R,i+1,t);    }}//直接选择排序 //对R[0...n-1]按递增有序进行直接选择排序void SelectSort(RecType R[],int n)    {    int i,j,k;    RecType tmp;    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!=j)        {            tmp=R[i];            R[i]=R[k];            R[k]=tmp;        }    }} //堆排序,分为两个步骤//堆排序——调整堆void sift(RecType R[],int low,int high)    {    int i=low,j=2*i;    RecType tmp=R[i];    while(j<=high)    {        if(j<high&&R[j].key<R[j+1].key)        {            j++;        }        if(tmp.key<R[j].key)        {            R[i]=R[j];            i=j;            j=2*i;        }        else        {            break;        }    }    R[i]=tmp;} //堆排序——排序 //对R[0...n-1]按递增有序进行堆排序void HeapSort(RecType R[],int n) {    int i;    RecType tmp;    for(i=n/2;i>=1;i--)    {        sift(R,i,n);    }    for(i>n;i>=2;i--)    {        tmp=R[1];        R[1]=R[i];        R[i]=tmp;        sift(R,1,i-1);    }} //二路归并排序共有3个步骤 //二路归并排序-----1void 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];    }    free(R1);} //二路归并排序-----2void 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)    {        Merge(R,i,i+length-1,n-1);    }}//二路归并排序-----3//对R[0...n-1]按递增有序进行二路归并排序void MergeSort(RecType R[],int n){    int length;    for(length=1;length<n;length=2*length)    {            MergePass(R,length,n);    }}//基数排序//输出排序好的单链表 void DispLink(RecType1 *L) {    RecType1 *p=L->next;    while(p!=NULL)    {        printf("%d",p->next);        p=p->next;    }    printf("\n");}//p为待排序序列的单链表指针//r为基数//d为关键字位数void RadixSort(RecType1 *p,int r,int d){    RecType1 *head[MAXD],*tail[MAXD],*t;    int i,j,k;    for(i=0;i<=d-1;i++)    {        for(j=0;i<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;i<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?"个":"十"));        DisLink(p);    }}int main(){    return 0;}

各排序算法简要分析
这里写图片描述
欢迎关注我的微信个人订阅号
这里写图片描述
每天多学一点0.0

0 0
原创粉丝点击