馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
問題說明:
a. 程式處理 程序過久,每處理 32768 byte 資料約需 10 秒,測試資料為 366548 Bytes,所以全部處理完成約需 110 秒。
修改說明:
a. 先執 排序(qsort ),再執行搜尋程序(MTSearch_SongNo),修改歌曲搜尋程序,改為 2維搜尋法,資料處理時間約需 6 秒。
//==================================================
// Function Name : Search
// 說明 : 搜尋在 buffer 的位置
// Writer : Jim
// Date : 20131008
// Input : SS : 比對字串
// s_type: 搜尋法, 需先處理 排序
// 0 --> 循序搜尋法
// 1 --> 2維搜尋法
// Return : buffer 的位置, -1: 沒找到
//==================================================
int Search( char* SS, int s_type )
{
// 修改搜尋方式, 加速處理, 2維搜尋法
if( 1 == s_type )
{
char tmp[10];
unsigned int s_cnt = 0;
int s_index = g_Total/2;
int tmp_index = 0;
int s_max = g_Total;
int s_min = 0;
int cmp_status = 0;
int ret_val = -1;
SSno[MAX_NO] = 0;
// printf( "SSno: [%s] ", SS );
// 限制搜尋次數
while( s_cnt++ < g_Total )
{
memcpy( tmp, pIndex[s_index].No, MAX_NO );
tmp[MAX_NO] = 0;
// 比對編號
cmp_status = strcmp( tmp, SS );
// 找到
if( cmp_status == 0 )
{
// printf( "Get No: %s ", tmp );
// 回傳 song index
ret_val = s_index;
break;
}
// tmp > SS, index 往上移
else if( cmp_status > 0 )
{
// printf( "tmp > SSn [%s], index: %d ", tmp, s_index );
s_max = s_index;
s_index -= ((s_index-s_min)/2);
// printf( "tmp > SS: s_max: %d, s_min: %d, index: %d ", s_max, s_min, s_index );
}
// tmp < SS, index 往下移
else if( cmp_status < 0 )
{
// printf( "tmp < SS: [%s], index: %d ", tmp, s_index );
s_min = s_index;
s_index += ((s_max-s_index)/2);
// printf( "tmp < SSo: s_max: %d, s_min: %d, index: %d ", s_max, s_min, s_index );
}
// 超出索引範圍
if( s_index < 0 )
break;
if( s_index >= (int)g_Total )
break;
// 若 Index 沒改變,表示沒找到
if( tmp_index != s_index )
tmp_index = s_index;
else
{
// printf( "No Find: [%s], index: %d ", SS, s_index );
break;
}
}
return ret_val;
}
else
////////////////////////////
{
unsigned int i=0;
char tmp[10];
SSno[MAX_NO]=0;
// printf( "SS = [%s] ",SS );
// 開始 Search
for( i=0; i<g_Total; i++ )
{
if( pIndex[i].No[0]<0x30 ) //加快 Search 的速度
continue;
memcpy( tmp, pSIndex[i].No, MAX_NO );
tmp[MAX_NO]=0;
// printf( "tmp: %s ", tmp );
if( strcmp(tmp,SS) == 0 )
{ //找到
// printf( "Get Song: %s ", tmp );
return i;
}
} //end for
return -1;
}
return -1;
} //end Search
|
評分
-
查看全部評分

|