读书人

排序有关问题

发布时间: 2012-02-15 12:09:44 作者: rapoo

排序问题
我有一个数组double tmp[3] = {2.2,3.2,1.2}
还有一个数组double tmp_s[3][3]:
tmp_s[0,0]:0.987924
tmp_s[0,1]:0.139131
tmp_s[0,2]:0.068176
对应-> tmp[0]=2.2
tmp_s[1,0]:-0.134119
tmp_s[1,1]:0.988251
tmp_s[1,2]:-0.073292
对应-> tmp[1]=3.2
tmp_s[2,0]:-0.077573
tmp_s[2,1]:0.063263
tmp_s[2,2]:0.994978
对应-> tmp[2]=1.2
我想实现先对tmp[3]排序从大到小(3.2,2.2,1.2),然后对应的tmp_s的index也相应交换.
变化后:
tmp_s[0,0]:-0.134119
tmp_s[0,1]:0.988251
tmp_s[0,2]:-0.073292
对应-> tmp[0]=3.2
tmp_s[1,0]:0.987924
tmp_s[1,1]:0.139131
tmp_s[1,2]:0.068176
对应-> tmp[1]=2.2
tmp_s[2,0]:-0.077573
tmp_s[2,1]:0.063263
tmp_s[2,2]:0.994978
对应-> tmp[2]=1.2

如何做呢?


[解决办法]
算法自己设计了
排序很多函数的
都是基础
STL里面有 C里面也有
也可自己设计
不要偷懒
[解决办法]
我觉得利用struct,将tmp与tmp_s对应起来,然后排序,程序应该比较简单。
[解决办法]
程序相对比较简单,楼主还是自己写吧,我不把代码附上了
如果实在不行,再求救吧
[解决办法]
#include "stdio.h "
#include "stdlib.h "
#include "string.h "
#include "math.h "

void main()
{
double tmp_s[3][3];
double tmp[] = {2.2, 3.2, 1.2};
double *tmpBuffer = (double *)malloc(sizeof(tmp) + 128);
memset(tmpBuffer, 0, sizeof(tmp) +128);
memcpy(tmpBuffer, tmp, sizeof(tmp));

int bitMapLow = 0;
int bitMapHigh = 0;
int tmpIndex = 0;

tmp_s[0][0] = 0.987924;
tmp_s[0][1] = 0.139131;
tmp_s[0][2] = 0.068176;

tmp_s[1][0] = -0.134119;
tmp_s[1][1] = 0.988251;
tmp_s[1][2] = -0.073292;

tmp_s[2][0] = -0.077573;
tmp_s[2][1] = 0.063263;
tmp_s[2][2] = 0.994978;

for(int i = 0; i < (sizeof(tmp)/sizeof(double)); i++)
{
int index = (int)(tmp[i] * 10);
if(index > 31)
{
bitMapHigh = bitMapHigh | (1 < <index);
}
else
{
bitMapLow = bitMapLow | (1 < <index);
}
}

memset(tmp, 0, sizeof(tmp));
for(int i = 32; i > 0; i--)
{
int flag = bitMapHigh & (1 < < i);
if(flag != 0)
{
tmp[tmpIndex] = (i/10) + (i%10)*pow(10.0, -1);
tmpIndex ++;
}
}
for(int i = 32; i > 0; i--)
{
int flag = bitMapLow & (1 < < i);
if(flag != 0)
{
tmp[tmpIndex] = (i/10) + (i%10)*pow(10.0, -1);
tmpIndex ++;
}
}
char *tmpFirst = (char *)tmp_s;
char *tmpSecond = tmpFirst + sizeof(double)*3;
char *tmpThird = tmpSecond + sizeof(double)*3;
double *tmpDouble = (double *)malloc(sizeof(double) *3 + 128);
memset(tmpDouble, 0 , sizeof(double) *3 + 128);

int indexFirst = 0;
int indexSecond = 0;
int indexThird = 0;
for(int i = 0; i < (sizeof(tmp)/sizeof(double)); i++)
{
for(int j = 0; j <(sizeof(tmp)/sizeof(double)); j++)
{
if(tmp[i] == tmpBuffer[j])
{
if(!indexFirst)
{
indexFirst = j+1;


}
else if(!indexSecond)
{
indexSecond = j+1;
}
else if(!indexThird)
{
indexThird = j+1;
}
}
}
}


switch(indexFirst)
{
case 1:
memcpy(tmpDouble, tmpSecond, sizeof(double)*3);
if(indexSecond == 3)
{
memcpy(tmpThird, tmpSecond, sizeof(double)*3);
memcpy(tmpSecond, tmpDouble, sizeof(double)*3);
}
break;
case 2:
memcpy(tmpDouble, tmpFirst, sizeof(double)*3);
memcpy(tmpFirst, tmpSecond, sizeof(double)*3);
if(indexSecond == 1)
{
memcpy(tmpSecond, tmpDouble, sizeof(double)*3);
}
else
{
memcpy(tmpSecond, tmpThird, sizeof(double)*3);
memcpy(tmpThird, tmpDouble, sizeof(double)*3);
}
break;
case 3:
memcpy(tmpDouble, tmpFirst, sizeof(double)*3);
memcpy(tmpFirst, tmpThird, sizeof(double)*3);
if(indexThird == 1)
{
memcpy(tmpThird, tmpDouble, sizeof(double)*3);
}
else
{
memcpy(tmpThird, tmpSecond, sizeof(double)*3);
memcpy(tmpSecond, tmpDouble, sizeof(double)*3);
}
break;
default:
break;
}

printf( "============After Sort============\n ");
printf( "sort tmp: %.1f, %.1f, %.1f\n ", tmp[0], tmp[1], tmp[2]);
printf( "sort tmp_s:\n ");
printf( "%f, %f, %f\n ", tmp_s[0][0], tmp_s[0][1], tmp_s[0][2]);
printf( "%f, %f, %f\n ", tmp_s[1][0], tmp_s[1][1], tmp_s[1][2]);
printf( "%f, %f, %f\n ", tmp_s[2][0], tmp_s[2][1], tmp_s[2][2]);

free(tmpDouble);
free(tmpBuffer);
}
---------------------------
Output
============After Sort============
sort tmp: 3.2, 2.2, 1.2
sort tmp_s:
-0.134119, 0.988251, -0.073292
0.987924, 0.139131, 0.068176
-0.077573, 0.063263, 0.994978

给分, 谢谢

读书人网 >C语言

热点推荐