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;
    }

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

  • 相关阅读:
    (转)CString,int,string,char*之间的转换
    Nt函数原型
    VC 创建窗口
    设置网络延时大小
    利用寄存器进入栈值交换
    控制用户控件里面的值
    jquery animate()动画函数
    终于有点时间了
    JQuery学习笔记(三)遮罩层、阴影层
    jquery表格隔行换色
  • 原文地址:https://www.cnblogs.com/wxb20/p/6148593.html
Copyright © 2011-2022 走看看