c# 合并数组后排序输出

来源:互联网 发布:oracle清空表空间数据 编辑:程序博客网 时间:2024/05/16 05:48

 protected void Page_Load(object sender, EventArgs e)
    {
       
        int[] a ={ 4, 3 };
        int[] b ={ 1, 2 };
        int[] arr1 = { 1, 2, 3, 4 };
        int[] arr2 = { 2, 3, 45, 6, 7 };
        int[] arr3 = { 4, 5, 6, 7, 8 };
        int[] result = CompoundArray(arr1, arr2, arr3);
        foreach (int i in result)
            Response.Write(i);
        getArray2(a, b);
        getHeBingArray();
    }

1:
public int[] Maopao(int[] items)
{
      for(int i=0;i<items.Length;i++)
     {
       for(int j=0;j<items.Length-1-i;j++)
      {
         if(items[j]>items[j+1])
          {
            int temp=items[j];
            items[j] = items[j + 1];
            items[j + 1] = temp;
          }
      }
     }
     return items;
}
private void getArray2(int[] left, int[] right)
    {
        if (left == null || right == null)
        {
            throw new ArgumentException("not");
        }
        int[] result = new int[left.Length + right.Length];
        int mIdex = 0;
      
        for (int i = 0; i < result.Length; i++)
        {
            if (i < left.Length)
            {
                result[i] = left[mIdex++];
            }
            else
            {
                result[i] = right[i - left.Length];
            }


        }
        result=Maopao(result);
        foreach (int aa in result)
        {
            Response.Write(aa + "<br>");
        }

    }

转:
2:
  public static int[] CompoundArray(params int[][] arrays)
    {
        List<int> list = new List<int>();
        foreach (int[] arr in arrays)
            list.AddRange(arr);
        return list.ToArray();
    }
3:
    public void getHeBingArray()
    {
        int[] a = new int[12] { 1, 3, 5, 7, 9, 12,0,0,0,0,0,0};
        int[] b = new int[5] { 10, 8, 6, 4, 2 };
        b.CopyTo(a, 5);
        Array.Sort(a);
        foreach (int var in a)
        {
            Response.Write(var + "<br>");
        }

   
    }