OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2744|回复: 0

双关键字排序,检索算法

[复制链接]

5

主题

35

帖子

0

精华

高级会员

Rank: 4

积分
515
金钱
515
注册时间
2014-2-24
在线时间
131 小时
发表于 2016-10-16 12:41:58 | 显示全部楼层 |阅读模式
#include <stdio.h>
#include <stdlib.h>

#define u8        unsigned char
#define u16        unsigned int

#define N 10

typedef struct
{
        u8 da1;
        u8 da2;
        u8 da3;
}da;


//将有二个有序数列a[first...mid]和a[mid...last]合并。  
int mergearray(da *a, int first, int mid, int last,int cda1,int cda2)
{  
    int i = first, j = mid + 1;  
    int m = mid,   n = last;  
    int k = 0,cmp;  
        da tempda[N+1];
        cmp = -1;
                 
    while (i <= m && j <= n)  
    {  
                if(a[i].da1 <= a[j].da1)
                {       
                        if(a[i].da1 == a[j].da1)
                        {
                                if(a[i].da2 > a[j].da2)
                                {
                                        tempda[10].da1 = a[i].da1;
                                        tempda[10].da2 = a[i].da2;
                                        tempda[10].da3 = a[i].da3;
                                        a[i].da1 = a[j].da1;
                                        a[i].da2 = a[j].da2;
                                        a[i].da3 = a[j].da3;
                                        a[j].da1 = tempda[10].da1;
                                        a[j].da2 = tempda[10].da2;
                                        a[j].da3 = tempda[10].da3;
                                }
                        }
                        tempda[k].da1 = a[i].da1;
                        tempda[k].da2 = a[i].da2;
                        tempda[k].da3 = a[i].da3;
                        k++;i++;
                       
                }
                else
                {       
                        tempda[k].da1 = a[j].da1;
                        tempda[k].da2 = a[j].da2;
                        tempda[k].da3 = a[j].da3;
                        k++;j++;
                }
               
    }  
      
    while (i <= m)  
    {   
                tempda[k].da1 = a[i].da1;
                tempda[k].da2 = a[i].da2;
                tempda[k].da3 = a[i].da3;
                k++;i++;
        }
    while (j <= n)  
    {   
                tempda[k].da1 = a[j].da1;
                tempda[k].da2 = a[j].da2;
                tempda[k].da3 = a[j].da3;
                k++;j++;
        }
      
    for (i = 0; i < k; i++)  
    {   
                a[first+i].da1 = tempda[i].da1;
                a[first+i].da2 = tempda[i].da2;
                a[first+i].da3 = tempda[i].da3;
                if((a[i].da1==cda1)&&(a[i].da2==cda2)||(a[j].da1==cda1)&&(a[j].da2==cda2))
                                cmp = i;
        }
        return cmp;
}


int mergesort(da *a, int first, int last)  compact reentrant
{  
        int cmpda;
        if (first < last)  
    {  
        int mid = (first + last) / 2;  
        mergesort(a, first, mid);    //左边有序  
        mergesort(a, mid + 1, last); //右边有序  
        cmpda = mergearray(a, first, mid, last,24,15); //再将二个有序数列合并
    }
        return cmpda;
}


void main(void)
{
        u8 i,o;
        int reda;
        da cmpda[N]={{1,5,19},//1
                                 {3,76,2},//2
                                 {3,14,98},//3
                                 {24,15,64},//4
                                 {89,76,10},//5
                                 {1,218,7},//6
                                 {1,45,67},//7
                                 {1,25,67},//8
                                 {24,19,64},//9
                                 {1,67,10}};//10
        reda = mergesort(cmpda, 0 , N-1);
        while(1)
        {
                for(i=0;i<7;i++)
                        o=i;
        }
}
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-24 02:45

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表