Throughout this course, we have been learning not only the foundational constructs of computer programming but also how to apply these concepts to solve various simple problems and implement basic application features. While we have not explicitly referred to our programs as algorithms, that is precisely what they are. Recall that an algorithm is a well-defined sequence of instructions designed to perform a specific task or solve a particular problem. In this lesson, we will explore some basic yet essential algorithms: Accumulation, Min and Max, Swap, Shuffle, and Merge.
The study of algorithms is a central part of computer science because it equips students with the tools they need to approach problem-solving systematically. Understanding algorithms is not just about knowing a series of steps; it’s about developing the ability to think critically and analytically. This skill set is invaluable as it enables students to tackle a wide range of computational problems with confidence.
Please be aware that this activity will differ from our previous ones. Each algorithm will serve as its own standalone activity. However, you are welcome to implement them all within a single project if you prefer.
The accumulation algorithm iterates through a collection of elements and combines them using a specific operation to produce a single cumulative result. Note that we have implemented a sum algorithm previously. However, this accumulation can use any math operation to aggregate the results, not just addition.
int[] numbers = [ 1, 2, 3, 4, 5 ];
int totalSum = 0;
foreach (int number in numbers)
{
totalSum += number;
}
// Optional
Console.WriteLine($"The total sum is: {totalSum}");
Steps:
For a given collection:
Optionally, output the final accumulated result.
The min and max algorithm involves iterating through a collection of elements to find the smallest (minimum) and largest (maximum) values.
int[] numbers = [ 3, 1, 4, 1, 5, 9 ];
// Find min
int minValue = numbers[0];
foreach (int number in numbers)
{
if (number < minValue)
{
minValue = number;
}
}
// Find max
int maxValue = numbers[0];
foreach (int number in numbers)
{
if (number > maxValue)
{
maxValue = number;
}
}
// Optional
Console.WriteLine($"The minimum value is: {minValue}");
Console.WriteLine($"The maximum value is: {maxValue}");
Steps:
For a given collection:
Optionally, iutput the final minimum and maximum values.
Note
The swap algorithm is a fundamental operation in computer programming that involves exchanging the values of two variables. This simple yet powerful technique is essential for many other algorithms, particularly in sorting algorithms.
int[] numbers = [ 1, 2, 3, 4, 5 ];
int temp;
int indexA = 1; // Index of the first element to swap
int indexB = 3; // Index of the second element to swap
temp = numbers[indexA];
numbers[indexA] = numbers[indexB];
numbers[indexB] = temp;
Steps:
For a given collection:
The shuffle algorithm rearranges the elements of a collection in a random order. Understanding how to shuffle elements is vital in scenarios where randomization is required, such as in games or simulations. Here’s an example using the Fisher-Yates* shuffle algorithm:
int[] numbers = [ 1, 2, 3, 4, 5 ];
Random random = new Random();
for (int i = numbers.Length - 1; i > 0; i--)
{
int j = random.Next(0, i + 1);
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
// Optional: Iterate through array for verification
foreach (int number in numbers)
{
Console.Write(number + " ");
}
Steps:
For a given collection:
Optionally, iterate through the array to verify that the elements have been shuffled.
*You may notice that there are algorithms that are named after their inventors as a way to honor their contributions to the field of computer science and mathematics.
The merge algorithm is a fundamental operation in computer programming that involves combining two or more sorted sequences into a single sorted sequence. This version of the merge algorithm combines two arrays by placing the elements of the second array immediately after the elements of the first array.
int[] array1 = [ 1, 3, 5, 7 ];
int[] array2 = [ 2, 4, 6, 8 ];
int[] mergedArray = new int[array1.Length + array2.Length];
for (int i = 0; i < array1.Length; i++)
{
mergedArray[i] = array1[i];
}
for (int j = 0; j < array2.Length; j++)
{
mergedArray[array1.Length + j] = array2[j];
}
// Optional: Iterate through array for verification
foreach (int number in mergedArray)
{
Console.Write(number + " ");
}
Steps:
For two given collections:
Optionally, Iterate through the merged array to verify that the elements have been combined.
This version of the merge algorithm combines two lists, which can be more flexible and easier to work with compared to arrays.
List<int> list1 = [ 1, 3, 5, 7 ];
List<int> list2 = [ 2, 4, 6, 8 ];
List<int> mergedList = [];
foreach (int number in list1)
{
mergedList.Add(number);
}
foreach (int number in list2)
{
mergedList.Add(number);
}
Steps:
For two given collections:
Add()
method.Add()
method.Bonus Method: AddRange()
As an alternative, you can use the AddRange() method to achieve the same result more concisely. The AddRange() method allows you to add all elements from one list to another in a single call.
mergedList.AddRange(list1);
mergedList.AddRange(list2);