Last active
May 12, 2021 18:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | |
} | |
} | |
} |
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
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:
AddFirstArrayIntoSingleArray
andAddSecondArrayIntoSingleArray
that are pretty much doing the same thing. In line with the DRY principle, that could have been one function as shown below:So that you can use that one function on lines 10 and 11.
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.