Click here to Skip to main content
15,905,590 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi there, I want to add an int array into a sorted array in a way to remain sorted. here
is my code, but it doesn't work:
C#
namespace Sort_Int_Array
{
    class Sort
    {
        public int[] Firstarray;
        public int[] Secondarray;
        public Sort(int[] firstarray, int [] secondarray)
        {
           Firstarray = firstarray;
            Secondarray= secondarray;
        }
        public Sort(int[] firstarray)
        {
            Firstarray = firstarray;
        }
        public string Convert_to_String()
        {
            string s = "";
            for (int i = 0; i < Firstarray.Length; i++)
            {
                s += "," + Firstarray[i].ToString();
            }
            return s;
        }

      
        public void Array_Set_Value()
        {
            int N=0;
            int [] finalarray = new int [Firstarray.Length + Secondarray.Length];
            for (int i = 0; i < Firstarray.Length; i++)
            {
                for (int j=0; j<Secondarray.Length; j++)
                {
                    if (Firstarray[i]< Secondarray[j])
                    {
                        N=i+1;
                        Firstarray.CopyTo(finalarray, 0);
                        finalarray.SetValue(Secondarray[i], N);
                    }
                }
            }
        }
        }
    }
Posted

You can use list<int> for this. I think you are trying to sort from 2 sorted array.

C#
namespace Sort_Int_Array
{
    class Sort
    {
        public List<int> Firstarray = new List<int>();
        public List<int> Secondarray = new List<int>();
        public List<int> finalarray = new List<int>();
        public Sort(List<int> firstarray, List<int> secondarray)
        {
            Firstarray = firstarray;
            Secondarray = secondarray;
        }
        public Sort(List<int> firstarray)
        {
            Firstarray = firstarray;
        }
        public string Convert_to_String()
        {
            string s = "";
            for (int i = 0; i < finalarray.Count; i++)
            {
                s += finalarray[i].ToString()+",";
            }
            return s;
        }


        public void Array_Set_Value()
        {
            while (Firstarray.Count!=0 || Secondarray.Count!=0)
            {
                if (Firstarray.Count != 0 && Secondarray.Count != 0)
                {
                    if (Firstarray[0] <= Secondarray[0])
                    {
                        finalarray.Add(Firstarray[0]);
                        Firstarray.RemoveAt(0);
                    }
                    else
                    {
                        finalarray.Add(Secondarray[0]);
                        Secondarray.RemoveAt(0);
                    }
                }
                else if (Firstarray.Count!=0)
                {
                    finalarray.Add(Firstarray[0]);
                    Firstarray.RemoveAt(0);
                }
                else
                {
                    finalarray.Add(Secondarray[0]);
                    Secondarray.RemoveAt(0);
                }
            }
        }
    }
}

List<int> a = new List<int>();
List<int> b = new List<int>();
a.Add(3);
a.Add(10);
b.Add(1);
b.Add(20);
Sort_Int_Array.Sort so = new Sort_Int_Array.Sort(a,b);
so.Array_Set_Value();
Console.WriteLine(so.Convert_to_String());</int></int></int></int></int></int></int></int></int></int></int></int></int>
 
Share this answer
 
v7
First, your Sort method doesn't do anything like sorting at all.

Second, your Array_Set_Value method does do what it says. It creates a 3rd array, copies the first array to it, then does something very odd. This is WAY more code than what's needed to do an Append job.

Why don't you just create an array that's big enough to hold both of them, Array.Copy the first array to element 0 of the target array, then Array.Copy the second array to the target array element (Length of first array + 1).

Now, if you want it sorted, all you have to do is call the Array.Sort method on it.
 
Share this answer
 
If u want to sort a finalarray,copy ur Firstarray and Secondarray into finalarray and then simple ur
finalarray.sort method.

if u are creating comma separated string then add comma at the end

C#
public string Convert_to_String()
       {
           string s = "";
           for (int i = 0; i< Firstarray.Length; i++)
           {
               s +=   Firstarray[i].ToString()+ ",";
           }
           return s;
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900