比较高效地实现从两个不同数组中提取相同部分组成新的数组(只支持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

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