Skip to content

Instantly share code, notes, and snippets.

@Ezeji
Last active May 12, 2021 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ezeji/317b51a60f6b48419de18678ecfe0b19 to your computer and use it in GitHub Desktop.
Save Ezeji/317b51a60f6b48419de18678ecfe0b19 to your computer and use it in GitHub Desktop.
This algorithm focuses on merging two arrays and sorting them in increasing order.
class Program
{
private static List<int> SingleArray { get; set; } = new List<int>();
static void Main(string[] args)
{
int[] classA = { 13, 15, 19 };
int[] classB = { 11, 13, 18 };
AddFirstArrayIntoSingleArray(classA);
AddSecondArrayIntoSingleArray(classB);
MergeArraysInIncreasingOrder(SingleArray);
PrintArray(SingleArray);
}
public static void AddFirstArrayIntoSingleArray(int[] classA)
{
if (classA == null) { }
else
{
SingleArray.AddRange(classA);
}
}
public static void AddSecondArrayIntoSingleArray(int[] classB)
{
if (classB == null) { }
else
{
SingleArray.AddRange(classB);
}
}
public static void MergeArraysInIncreasingOrder(List<int> singleArray)
{
if (singleArray == null) { }
else
{
int max = singleArray.Max();
int min = singleArray.Min();
int range = max - min + 1;
int[] count = new int[range];
int[] output = new int[singleArray.Count];
for (int i = 0; i < singleArray.Count; i++)
{
count[singleArray[i] - min]++;
}
for (int i = 1; i < count.Length; i++)
{
count[i] += count[i - 1];
}
for (int i = singleArray.Count - 1; i >= 0; i--)
{
output[count[singleArray[i] - min] - 1] = singleArray[i];
count[singleArray[i] - min]--;
}
for (int i = 0; i < singleArray.Count; i++)
{
singleArray[i] = output[i];
}
}
}
public static void PrintArray(List<int> singleArray)
{
if (singleArray == null) { }
else
{
for (int i = 0; i < singleArray.Count; i++)
{
Console.Write(singleArray[i] + " ");
}
Console.WriteLine("");
}
}
}
@meekg33k
Copy link

meekg33k commented May 12, 2021

Hello @Ezeji, thank you for participating in Week 5 of #AlgorithmFridays.

This is a decent attempt at solving the problem and I like your smart approach of concatenating both arrays and then sorting them. I thought to highlight a couple of things:

  • In terms of code cleanness, you have two functions AddFirstArrayIntoSingleArray and AddSecondArrayIntoSingleArray that are pretty much doing the same thing. In line with the DRY principle, that could have been one function as shown below:
        public static void AddArrayIntoSingleArray(int[] className)
        {

            if (className== null) { }
            else
            {
                SingleArray.AddRange(className);
            }
        }

So that you can use that one function on lines 10 and 11.

  • Another thing I'll like to know is what you think about the memory efficiency of your solution because I see you have created two extra arrays (lists) on lines 56 and 57.

That said in terms of correctness, this was a good shot. Kudos to you!

Do let me know what you think with regards to the feedback and thanks once again for participating.

@Ezeji
Copy link
Author

Ezeji commented May 12, 2021

Hello @meekg33k

Thanks for the feedback. Really, they're helpful as I didn't consider those being that they were an oversight.

Thanks once again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment