排序的概念及分类实现

1.#include <stdio.h>
/* 实现多关键字排序 */
typedef struct _tag_DataElem
{
    char desc[20];
    int key1;
    int key2;
} DataElem;

int compare1(DataElem* ld, DataElem* rd)
{
    int ret = 0;
    
    if( ld->key1 > rd->key1 )
    {
        ret = 1;
    }
    else if( ld->key1 == rd->key1 )
    {
        if( ld->key2 > rd->key2 )
        {
            ret = 1;
        }
        
        if( ld->key2 < rd->key2 )
        {
            ret = -1;
        }
    }
    else
    {
        ret = -1;
    }
    
    return ret;
}
//
int compare2(DataElem* ld, DataElem* rd)
{
    return (ld->key1*100 + ld->key2) - (rd->key1*100 + rd->key2);
}

int main()
{
    //初始化结构体data  大于返回负数 否则
    DataElem d1 = {"d1", 91, 80};
    DataElem d2 = {"d2", 91, 88};
    //输出结果
    printf("Compare1 %s and %s: %d\n", d1.desc, d2.desc, compare1(&d1, &d2));
    printf("Compare2 %s and %s: %d\n", d1.desc, d2.desc, compare2(&d1, &d2));
    
    return 0;
}

/*  内排序  与  外排序  时间性能  存储空间  复杂性  */
/*  空间换时间   时间换空间   */

 

posted @ 2016-12-09 10:51  王小波私人定制  阅读(241)  评论(0编辑  收藏  举报
DON'T FORGET TO HAVE FUN