綁定帳號登入

Android 台灣中文網

tag 標籤: 實做

相關日誌

分享 二維搜尋法 實做 Sample
jianrupan 2013-10-29 13:45
問題說明: a. 程式處理 程序過久,每處理 32768 byte 資料約需 10 秒 ,測試資料為 366548 Bytes ,所以全部處理完成約需 110 秒 。 修改說明: a. 先執 排序( qsort ),再執行搜尋程序( MTSearch_SongNo ), 修改歌曲搜尋程序,改為 2 維搜尋法 ,資料處理時間約需 6 秒。 //================================================== // Function Name : MTSearch // 說明:搜尋歌號在 buffer 歌單 的位置 // Writer:Jim // Date:20131008 // Input: SS : 比對字串 // s_type: 搜尋法, 需先處理 歌單排序 // 0 -- 循序搜尋法 // 1 -- 2維搜尋法 // Return: buffer的位置, -1: 沒找到 //================================================== int MTSearch( char* SS, int s_type ) { // 修改搜尋方式, 加速處理, 2維搜尋法 if( 1 == s_type ) { char tmp ; 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 = 0; // printf( "SSno: ", SS ); // 限制搜尋次數 while( s_cnt++ g_Total ) { memcpy( tmp, pIndex .No, MAX_NO ); tmp = 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 , 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: , 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: , index: %d ", SS, s_index ); break; } } return ret_val; } else //////////////////////////// { unsigned int i=0; char tmp ; SSno =0; // printf( "SS = ",SS ); // 開始 Search for( i=0; ig_Total; i++ ) { if( pIndex .No 0x30 )//加快 Search 的速度 continue; memcpy( tmp, pSIndex .No, MAX_NO ); tmp =0; // printf( "tmp: %s ", tmp ); if( strcmp(tmp,SS) == 0 ) {//找到 // printf( "Get Song: %s ", tmp ); return i; } } //end for return -1; } return -1; } //end MTSearch_SongNo
個人分類: 軟體應用|209 次閱讀|0 個評論