zoukankan      html  css  js  c++  java
  • 排序的概念及分类实现

    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 ", d1.desc, d2.desc, compare1(&d1, &d2));
        printf("Compare2 %s and %s: %d ", d1.desc, d2.desc, compare2(&d1, &d2));
        
        return 0;
    }

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

  • 相关阅读:
    最好的在线打字练习网站
    input 的 type 等于 file
    windows 删除文件或文件夹
    nvm 管理 node 版本
    github 的 fork 取消功能
    window cmd 命令行下创建文件夹和文件
    17_10_11 Redis 指令
    17_10_11 Mac 上的brew 安装指令
    17_10_11 运算符&,&&,>> 和 >>>
    17_10_10 乱码问题总结
  • 原文地址:https://www.cnblogs.com/wxb20/p/6148593.html
Copyright © 2011-2022 走看看