![]() |
Intro | 6.1: Array | 6.2: Traversing | 6.3: Enhanced for | 6.4: Algos |
Unit 6.4 - Arrays
Unit 6 Team Teach - Arrays
6.4 Developing Algorithms Using Arrays
Finding Maximum and Minimum of Arrays
We’ve done this in python many times in the past, but in java it will be different. Steps:
- Initialize a variable to store a maximum value
- Iterate through every element in the list
- In python we would use
for x in listbut this isn’t possible now, instead we must use an enhanced for loop
- In python we would use
- Check if element is greater than max, if so replace
- return element
/* function, takes in array of doubles as values */
private double findMax(double [] values) {
/* Sets max to first number in the list. Min Value could be the min value */
double max = values[0];
/* enhanced for loop notation */
for (double value : values) {
/* check for max */
if (value > max) {
max = value;
}
}
return max;
}
/* run on array */
double[] nums = {1,2,3,4,5,6,2000, 123.123, 1230912839018230.123901823};
System.out.println(findMax(nums));
1.23091283901823E15
Popcorn Hack
Implement a function to find the minimum of the EVEN INDEXED elements in array of integers.
private double findMinEven(int [] values) {
if (values.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
double min = Double.MAX_VALUE;
for (int i = 0; i < values.length; i += 2) {
if (values[i] < min) {
min = values[i];
}
}
return min;
}
int[] nums = {5, 3, 8, 1, 4, 7, 2, 9};
System.out.println(findMinEven(nums)); // Output should be 2
2.0
Ok wait but i don’t want to copy this every time. what happened to python’s .max?
import java.util.Arrays;
public class ArrayUtils {
public static double findMinEven(int[] values) {
if (values.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
double min = Double.MAX_VALUE;
for (int i = 0; i < values.length; i += 2) {
if (values[i] < min) {
min = values[i];
}
}
return min;
}
public static double findMax(double[] values) {
return Arrays.stream(values).max().getAsDouble();
}
}
// Usage
int[] nums = {34, 2, 432, 4, 42, 72342, 22, 94};
System.out.println(ArrayUtils.findMinEven(nums));
double[] numsDouble = {12348, 2234, 367, 54, 5567, 656, 23497};
System.out.println(ArrayUtils.findMax(numsDouble));
22.0
23497.0
Shifting Arrays
Oftentimes we need to shift an array. ex. Shifting an array right two: Original: {“a”, “b”, “c”, “d”} Final: {“d”, “c”, “a”, “b”}
How do we do this?
- Create a new array
- Iterate through each element in the array
- Place in appropriate spoce
int [] numbers = {1,2,3,4,5,6,7,8,9,10};
int shift = 8;
/* function */
private int [] shiftRight(int [] values, int shift) {
/* declare new array */
int [] shifted = new int [values.length];
/* iterate through each array */
for (int index = 0; index < values.length; index++) {
/*
Breakdown:
shifted [new index] = numbers[index] (old value)
WAIT! but i'm adding a value to somewhere in the middle of the array!
we couldn't do that in python but since we specified the type and length of the array, this is possible in java
Calculating the new index:
1. we add the shift to the index
2. handle overflow: we use % to take the modulo operation
*/
shifted [Math.abs((index + shift) % numbers.length)] = numbers[index];
}
return shifted;
}
for ( int value : shiftRight(numbers, shift)) {
System.out.println(value);
}
3
4
5
6
7
8
9
10
1
2
Challenge Hack: Create a function that iterates through every X items and shifts only those elements Y shift left.
Example: ({1, 2, 3, 4, 5, 6}, Y = 1, X = 2) Output: {5, 2, 1, 4, 3, 6}
Example: ({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, Y = 2, X = 3) Output: {10, 2, 3, 1, 5, 6, 4, 8, 9, 7}
int [] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int shift = 2;
int space = 4;
/* function */
private int[] shiftLeft(int[] values, int shift, int space) {
int[] shifted = new int[values.length];
for (int i = 0; i < values.length; i++) {
int groupIndex = (i / space) * space; // Calculate the starting index of the current group
int posInGroup = i % space; // Determine position within the group
int newPosInGroup = (posInGroup + shift) % space; // Calculate the new shifted position in the group
shifted[i] = values[groupIndex + newPosInGroup]; // Assign the shifted value
}
return shifted;
}
for ( int value : shiftLeft(numbers, shift, space)) {
System.out.println(value);
}
3
4
1
2
7
8
5
6
11
12
9
10
15
16
13
14
19
20
17
18
