註冊 登錄
Android 台灣中文網 返回首頁

jianrupan的個人空間 https://apk.tw/?1180935 [收藏] [複製] [分享] [RSS]

日誌

二維搜尋法 實做 Sample

已有 211 次閱讀2013-10-29 13:45 |個人分類:軟體應用| 二維搜尋法, 實做, Sample

問題說明:

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[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 MTSearch_SongNo

路過

雞蛋

鮮花

握手

雷人

評論 (0 個評論)

facelist

您需要登錄後才可以評論 登錄 | 註冊