6 min read•december 29, 2022
Athena_Codes
Milo Chang
Athena_Codes
Milo Chang
Using the that we have learned in the previous two topics, we can make many useful algorithms. Here, we will have a snippet for each you are expected to know with annotations for each.
You should come up with an example 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
Initializing the maxValue and to 0 is a common mistake.
If all the values in the are positive, it would incorrectly keep at 0 (all the values are greater than 0, leaving 0 as the minimum).
If all the values in the are negative, it would incorrectly keep maxValue at 0 (all the values are less than 0, leaving 0 as the ).
To counter these errors, initialize these to the first value in the .
/** Sums up all elements in the
/** Finds the
/** Finds the
/** Determines whether all values are even
*/
public static boolean
/** Returns all consecutive sequences of length n in the
/** Checks to see if there are duplicate elements
*/
public static boolean
/** Returns how many even numbers there are
*/
public static int
/** Shifts Elements One Index to the Left
*/
public static int[]
/** Shifts Elements One Index to the Right
*/
public static int[]
/** Reverses the
Algorithm
: Algorithms are step-by-step procedures or instructions designed to solve specific problems or perform specific tasks.Array
: 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.duplicates
: Elements or values that appear more than once in a collection, such as an array or list.evenFrequency
: Even frequency refers to the occurrence of an element in a list or array that appears an equal number of times as other elements. In other words, every element with even frequency occurs the same number of times.isEven
: A boolean function that determines whether a given number is divisible by 2 without leaving a remainder.Maximum
: The maximum refers to the largest value in a set of numbers or the highest point on a graph.mean
: Mean, also known as average, is calculated by adding up all values in a set and dividing it by the total number of values.minValue
: The minValue refers to the smallest value in a set of numbers or data.Mode
: The mode refers to the value that appears most frequently in a set of data.returnAllConsecutiveSequences
: A function that takes an array as input and returns all consecutive sequences (subarrays) within it.Reverse
: Reversing a string means to change the order of its characters, so that the last character becomes the first, the second-to-last becomes the second, and so on.shiftLeft
: Shift left refers to moving all elements in a list or array towards the left by one position. The first element becomes the last element after shifting left.shiftRight
: Shift right refers to moving all elements in a list or array towards the right by one position. The last element becomes the first element after shifting right.sum
: Sum refers to the result obtained by adding two or more numbers together.Traversals
: Traversals refer to the process of visiting and accessing each element in a data structure, such as a tree or graph, exactly once.6 min read•december 29, 2022
Athena_Codes
Milo Chang
Athena_Codes
Milo Chang
Using the that we have learned in the previous two topics, we can make many useful algorithms. Here, we will have a snippet for each you are expected to know with annotations for each.
You should come up with an example 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
Initializing the maxValue and to 0 is a common mistake.
If all the values in the are positive, it would incorrectly keep at 0 (all the values are greater than 0, leaving 0 as the minimum).
If all the values in the are negative, it would incorrectly keep maxValue at 0 (all the values are less than 0, leaving 0 as the ).
To counter these errors, initialize these to the first value in the .
/** Sums up all elements in the
/** Finds the
/** Finds the
/** Determines whether all values are even
*/
public static boolean
/** Returns all consecutive sequences of length n in the
/** Checks to see if there are duplicate elements
*/
public static boolean
/** Returns how many even numbers there are
*/
public static int
/** Shifts Elements One Index to the Left
*/
public static int[]
/** Shifts Elements One Index to the Right
*/
public static int[]
/** Reverses the
Algorithm
: Algorithms are step-by-step procedures or instructions designed to solve specific problems or perform specific tasks.Array
: 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.duplicates
: Elements or values that appear more than once in a collection, such as an array or list.evenFrequency
: Even frequency refers to the occurrence of an element in a list or array that appears an equal number of times as other elements. In other words, every element with even frequency occurs the same number of times.isEven
: A boolean function that determines whether a given number is divisible by 2 without leaving a remainder.Maximum
: The maximum refers to the largest value in a set of numbers or the highest point on a graph.mean
: Mean, also known as average, is calculated by adding up all values in a set and dividing it by the total number of values.minValue
: The minValue refers to the smallest value in a set of numbers or data.Mode
: The mode refers to the value that appears most frequently in a set of data.returnAllConsecutiveSequences
: A function that takes an array as input and returns all consecutive sequences (subarrays) within it.Reverse
: Reversing a string means to change the order of its characters, so that the last character becomes the first, the second-to-last becomes the second, and so on.shiftLeft
: Shift left refers to moving all elements in a list or array towards the left by one position. The first element becomes the last element after shifting left.shiftRight
: Shift right refers to moving all elements in a list or array towards the right by one position. The last element becomes the first element after shifting right.sum
: Sum refers to the result obtained by adding two or more numbers together.Traversals
: Traversals refer to the process of visiting and accessing each element in a data structure, such as a tree or graph, exactly once.© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.