Developing Algorithms Using Arrays

Emily Wilson
6 min read
Listen to this study note
Study Guide Overview
This study guide covers standard array algorithms including finding the minimum, maximum, sum, mean, and mode. It also explains how to determine if all elements satisfy a condition (e.g., all even), work with consecutive sequences, check for duplicates, count elements meeting specific criteria, and perform left/right shifts and array reversal. Example code snippets in Java with explanations are provided for each algorithm.
#Standard Algorithms
Using the traversals that we have learned in the previous two topics, we can make many useful algorithms. Here, we will have a snippet for each algorithm you are expected to know with annotations for each.
You should come up with an example array to use as you trace through the code below. This will help you gain a better understanding of how the algorithms work by allowing you to see the loops in action and what they are manipulating.
#Finding the Minimum and Maximum
/** Finds the maximum
*/
public static int maximum(int[] array) {
int maxValue = array[0];
for (int number: array) {
if (number > maxValue) {
//if new max value found, replace current maxValue
maxValue = number;
}
}
return maxValue;
}
/** Finds the minimum
*/
public static int minimum(int[] array) {
int minValue = array[0];
for (int number: array) {
if (number < minValue) {
//if new min value found, replace current minValue
minValue = number;
}
}
return minValue;
}
Initializing the maxValue and minValue to 0 is a common mistake.
- If all the values in the array are positive, it would incorrectly keep minValue at 0 (all the values are greater than 0, leaving 0 as the minimum).
- If all the values in the array are negative, it would incorrectly keep maxValue at 0 (all the values are less than...

How are we doing?
Give us your feedback and let us know how we can improve