4 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
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.
/** Finds the [maximum](https://www.fiveableKeyTerm: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](https://www.fiveableKeyTerm: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.
/** Sums up all elements in the array
*/
public static int [sum](https://www.fiveableKeyTerm:sum)(int[] array) {
int sum = 0;
for (int number: array) {
sum += number; //adds every element to sum
}
return sum;
}
/** Finds the [mean](https://www.fiveableKeyTerm:mean)/average of the array
*/
public static int mean(int[] array) {
// find the sum of the array, can be replaced with sum algorithm from above
int sum = sum(array);
// then divide by number of items
return (double) sum / (array.length);
}
/** Finds the [mode](https://www.fiveableKeyTerm:Mode) of an array
**Prerequisite:** The array must have a mode
*/
public static int mode(int[] array) {
int mostCommon = 0;
int mostCommonFrequency = 0;
//traverse through the first n-1 elements of the array
for (int i = 0; i < array.length - 1; i++) {
int currentFrequency = 1;
//traverse through the array starting from the next element
for (int j = i + 1; j < array.length; j++) {
if (array[j] == array[i]) {
// if any element matches current element being checked, add 1 to frequency
currentFrequency++;
}
}
if (currentFrequency > mostCommonFrequency) {
// replaces current mode if new most common element
mostCommon = array[i];
mostCommonFrequency = currentFrequency;
}
}
return mostCommon; // can also be modified to return the frequency
}
/** Determines whether all values are even
*/
public static boolean [isEven](https://www.fiveableKeyTerm:isEven)(int[] array) {
//Assume all values are positive first
for (int number: array) {
if (number % 2 == 1) {
//If there is one value that is not positive, return false
return false;
}
}
return true; //No odd numbers were found
}
/** Returns all consecutive sequences of length n in the array
*/
public static void [returnAllConsecutiveSequences](https://www.fiveableKeyTerm:returnAllConsecutiveSequences)(int[] array, int length) {
for (int i = 0; i <= array.length - length; i++) {
for (int j = 0; j < length; j++) {
//2 loops, one to get the starting number, the other to go through the sequences
System.out.print(array[i+j] + " ");
}
System.out.println();
}
}
/** Checks to see if there are duplicate elements
*/
public static boolean [duplicates](https://www.fiveableKeyTerm:duplicates)(int[] array) {
for (int i = 0; i < array.length - 1; i++) { //traverse through the array
for (int j = i + 1; j < array.length; j++) { //traverse through rest of array
if (array[j] == array[i]) {
// if any element matches current element being checked, return true
return true;
}
}
}
return false; // if this point reached, no duplicates found
}
/** Returns how many even numbers there are
*/
public static int [evenFrequency](https://www.fiveableKeyTerm:evenFrequency)(int[] array) {
int numberEven = 0;
for (int number: array) {
if (number % 2 == 0) {
// increments every time an even integer is found
numberEven++;
}
}
return numberEven;
}
/** Shifts Elements One Index to the Left
*/
public static int[] [shiftLeft](https://www.fiveableKeyTerm:shiftLeft)(int[] array) {
int firstItem = array[0]
for (int i = 0; i < array.length - 1; i++) {
// Does the shifting
array[i] = array[i+1];
}
array[array.length - 1] = firstItem;
return array;
}
/** Shifts Elements One Index to the Right
*/
public static int[] [shiftRight](https://www.fiveableKeyTerm:shiftRight)(int[] array) {
int lastItem = array[array.length - 1]
for (int i = array.length - 1; i > 0; i--) {
// Does the shifting
array[i] = array[i-1];
}
array[0] = lastItem;
return array;
}
/** Reverses the array
*/
public static int[] [reverse](https://www.fiveableKeyTerm:Reverse)(int[] array) {
int[] newArray = new int[array.length];
for (int i = 0; i < array.length; i++) {
// places the items in the new array in opposite order of the original
newArray[i] = array[array.length - i - 1];
}
return newArray;
}
Algorithms are step-by-step procedures or instructions designed to solve specific problems or perform specific tasks.
Term 1 of 15
Algorithms are step-by-step procedures or instructions designed to solve specific problems or perform specific tasks.
Term 1 of 15
Algorithms are step-by-step procedures or instructions designed to solve specific problems or perform specific tasks.
Term 1 of 15
Traversals refer to the process of visiting and accessing each element in a data structure, such as a tree or graph, exactly once.
Depth-First Search (DFS): An algorithm that explores as far as possible along each branch before backtracking. It is often used for traversing trees or graphs.
Breadth-First Search (BFS): An algorithm that explores all the vertices of a graph at the same level before moving on to the next level. It is commonly used for finding shortest paths.
In-order traversal: A type of traversal specifically used for binary trees where nodes are visited in ascending order based on their values.
Algorithms are step-by-step procedures or instructions designed to solve specific problems or perform specific tasks.
Pseudocode: A simplified programming language-like notation used to describe algorithms without getting into specific syntax details.
Sorting Algorithms: Algorithms designed to arrange elements in a particular order, such as alphabetical or numerical order.
Searching Algorithms: Algorithms used to find the location of a specific element within a collection of data.
An array is a fixed-size collection of elements of the same type stored in contiguous memory locations. It allows efficient access to individual elements using an index.
Indexing: Indexing refers to accessing individual elements within an array by specifying their position using an index value.
Length/Size: The length or size represents the total number of elements present in an array.
Multidimensional Array: A multidimensional array is an array with multiple dimensions (rows and columns) that allows storing data in tabular form.