比較高效地實現從兩個不同數組中提取相同部分組成新的數組(只支持Int類型) [C#]...

        有時候我們需要從兩個不同數組中提取出相同的部分的數組或者計算有多少個相同的項,這個算法剛好能派上用場,

實現方案:
1、將兩個數組按從小到大排序;
2、遍歷第一個數組 array1,跟第二個數組 array2 做比較;
3、如果找到相等的則提取出該數據並且記錄下 array2 的下標到臨時變量 t,下次循環則從 array2[t+1] 開始遍歷 array2;
4、如果沒有匹配到相等的而且 array2[t] 大於與 array1 對比的數據時, 記下 array2 當前下標到臨時變量 t,下次循環則從 array2[t] 開始;

經過測試,該算法比“最笨”的作法無法在數組以何種方式排序都要強,如果量越多會越明顯,有點可惜的是暫時只能使用在 Int[] 上,有時間再想一下其它數據類型的比較

ContractedBlock.gifExpandedBlockStart.gif獲取兩個整型數組中相等項的集合
ContractedBlock.gifExpandedBlockStart.gif獲取兩個整型數組中相等項的集合#region  獲取兩個整型數組中相等項的集合
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif
/// 獲取兩個整型數組中相等項的集合
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="array1"></param>
InBlock.gif
/// <param name="array2"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic static int[] CompareEquation(int[] array1, int[] array2)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
int i1, i2;
InBlock.gif    
// 記錄第二個數組上一次匹配到的位置
InBlock.gif
    int t = 0;
InBlock.gif    
// 記錄相同的項
InBlock.gif
    List<int> equal = new List<int>();
InBlock.gif
InBlock.gif    
int[] sort1 = BubbleSort(array1);
InBlock.gif    
int[] sort2 = BubbleSort(array2);
InBlock.gif
InBlock.gif    
for (int i = 0; i < sort1.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        i1 
= sort1[i];
InBlock.gif
InBlock.gif        
for (int j = t; j < sort2.Length; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            i2 
= sort2[j];
InBlock.gif
InBlock.gif            
if (i2 == i1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                equal.Add(i2);
InBlock.gif                
// 下次比較從下一位開始
InBlock.gif
                t = j + 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (i2 > i1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 下次比較繼續從這裏開始
InBlock.gif
                t = j;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return equal.ToArray();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif
#endregion

ContractedBlock.gifExpandedBlockStart.gif冒泡法排序(非原創)
ContractedBlock.gifExpandedBlockStart.gif冒泡法排序(非原創)#region 冒泡法排序(非原創)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif
/// 冒泡法排序
InBlock.gif
/// </summary>
InBlock.gif
/// <returns>排序結果: 從小到大(升序)</returns>
ExpandedSubBlockEnd.gif
/// <see cref="http://www.aspcool.com/lanmu/browse1.asp?ID=1223&bbsuser=csharp"/>

InBlock.gifpublic static int[] BubbleSort(int[] R)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
int i, j, temp;
InBlock.gif    
//交換標誌 
InBlock.gif
    bool exchange;
InBlock.gif    
//最多做R.Length-1趟排序 
InBlock.gif
    for (i = 0; i < R.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//本趟排序開始前,交換標誌應爲假 
InBlock.gif
        exchange = false;
InBlock.gif        
for (j = R.Length - 2; j >= i; j--)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//交換條件 
InBlock.gif
            if (R[j + 1< R[j])
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                temp 
= R[j + 1];
InBlock.gif                R[j 
+ 1= R[j];
InBlock.gif                R[j] 
= temp;
InBlock.gif                
//發生了交換,故將交換標誌置爲真 
InBlock.gif
                exchange = true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//本趟排序未發生交換,提前終止算法 
InBlock.gif
        if (!exchange)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
break;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return R;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif
#endregion

測試結果:



所謂的最笨的方法:

ContractedBlock.gifExpandedBlockStart.gif自我感覺最笨的作法
ContractedBlock.gifExpandedBlockStart.gif計算兩個整型數組中數值相等的數量(最笨的方法)#region 計算兩個整型數組中數值相等的數量(最笨的方法)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif
/// 計算兩個整型數組中數值相等的數量(最笨的方法)
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="array1"></param>
InBlock.gif
/// <param name="array2"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic static int[] CompareEquation1(int[] array1, int[] array2)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    List
<int> equal = new List<int>();
InBlock.gif
InBlock.gif    
foreach (int i1 in array1)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
foreach (int i2 in array2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (i1 == i2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                equal.Add(i1);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return equal.ToArray();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif
#endregion

從圖中可以看到運行效率有了很大的提升,
以上只是不成熟的解決方案,歡迎各位一起來討論! 22.gif

相關代碼下載
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章