孝感人口数量2016:结构体排序算法

来源:百度文库 编辑:高考问答 时间:2024/05/01 11:37:57
在C语言中怎样怎样按照其中一项来排序
怎样实现呢?
麻烦高手指导,最好是原代码
谢谢了

typedef struct datuser
{
INT8 usernm[USRNAMELENS];
INT8 passwd[PASSWRDLENS];
INT8 addess[USRADDRLENS];
}DATUSER;

DATUSER*userbase_buf=NULL;

volatile INT32 *userinfp=NULL; //record &userbase_buf[i]
volatile INT32*guserpos=NULL; //record i
---------------------------------------------------------------------
userinfp[0]=(INT32)(&userbase_buf[0]);
guserpos[0]=0;
----------------------------------------------------------------------
void dfindzoneuser(INT8 *keyname,INT32 vlen,INT32 *head,INT32 *tail)
{//1/2 method to find user
int ret = -1;
int mid, low=-1, high=vlen;
/*
[9,8,7,6,5,4,3,2,1]
L H
*/
do {
mid = ((low+high)>>1);
//big-->small(key compare orderd-array)
ret=strcmp((const char *)keyname,(const char *)(((DATUSER *)userinfp[mid])->usernm));
//small-->big(orderd-array compare key)
//ret=strcmp((const char *)(((DATUSER *)userinfp[mid])->usernm),(const char *)name);
if (ret<0) {
low = mid;
} else if (ret>0) {
high = mid;
} else {
*head=mid;
*tail=mid;
return;
}
}while((high-low)>1);
*head=low;
*tail=high;
}
------------------------------
register int i,diff,headplus;
int head,tail;
for(i=1;i<DB_USERINF_CNT;i++){
if(0==userbase_buf[i].usernm[0]){
break;
}
dfindzoneuser(userbase_buf[i].usernm,i,&head,&tail);//二分法查找应该放在哪个区间
/*
means:the i must be (head,tail)
maybe:(N,N)such as(0,0),so we save space(1) to i
maybe:(-1,0)must it shuld be first,so we save space0 to i
maybe:(0,1)must it shuld be second,so we save space1 to i
*/
headplus=(head+1);
diff=(i-headplus);
if(diff){
memmove(userinfp+headplus+1,userinfp+headplus,diff*UINT32SIZE);
memmove(guserpos+headplus+1,guserpos+headplus,diff*UINT32SIZE);
}
userinfp[headplus]=(INT32)(&userbase_buf[i]);
guserpos[headplus]=i;
}
----------------------------------------------
主要思想
1:记录结构体地址,移动地址数组以减少损耗。
2:一边1/2法查,一边加数据,以此循环。

用数组实现链状存储结构

用数组存储结构指针然后再排序就好了

r5nnjguig