1// Find-Max variant -- rather than finding the max value, find the
2// *index* of the max value
3public int findMaxIndex(int[] nums) {
4 int maxIndex = 0; // say the 0th element is the biggest
5 int maxValue = nums[0];
6
7 // Look at every element, starting at 1
8 for (int i=1; i<nums.length; i++) {
9 if (nums[i] > maxValue) {
10 maxIndex = i;
11 maxValue = nums[maxIndex];
12 }
13 }
14 return maxIndex;
15}
16
1int findSmallest(int[] values) {
2 int minIndex = 0; // start with 0th element as min
3 for (int i=1; i<values.length; i++) {
4 if (values[i] < values[minIndex]) {
5 minIndex = i;
6 }
7 }
8 return minIndex;
9}
10
1// For-All variant
2// Go through the elements backwards
3// by adjusting the for-loop
4public void forAllBackwards(int[] nums) {
5 for (int i = nums.length-1; i>=0; i--) {
6 System.out.println( nums[i] );
7 }
8}
9
1// Find-Max variant
2// Use very small number, Integer.MIN_VALUE,
3// as the initial max value.
4public int findMax2(int[] nums) {
5 int maxSoFar = Integer.MIN_VALUE; // about -2 billion
6
7 // Look at every element
8 for (int i=0; i<nums.length; i++) {
9 if (nums[i] > maxSoFar) {
10 maxSoFar = nums[i];
11 }
12 }
13
14 return maxSoFar;
15}
16
1String[] words = new String[] { "hello", "foo", "bar" };
2int[] squares = new int[] { 1, 4, 9, 16 };
3
1// Given an array of booleans representing a series
2// of coin tosses (true=heads, false=tails),
3// returns true if the array contains anywhere within it
4// a string of 10 heads in a row.
5// (example of a search loop)
6public boolean searchHeads(boolean[] heads) {
7 int streak = 0; // count the streak of heads in a row
8
9 for (int i=0; i<heads.length; i++) {
10 if (heads[i]) { // heads : increment streak
11 streak++;
12 if (streak == 10) {
13 return true; // found it!
14 }
15 }
16 else { // tails : streak is broken
17 streak = 0;
18 }
19 }
20
21 // If we get here, there was no streak of 10
22 return false;
23}
24
1// Search
2// Searches through the given array looking
3// for the given target. Returns the index number
4// of the target if found, or -1 otherwise.
5// (classic search-loop example)
6public int search(int[] nums, int target) {
7 // Look at every element
8 for (int i=0; i<nums.length; i++) {
9 if (nums[i] == target) {
10 return i; // return the index where the target is found
11 }
12 }
13
14 // If we get here, the target was not in the array
15 return -1;
16}
17
1// For-All
2// Do something for every element
3public void forAll(int[] nums) {
4 for (int i=0; i<nums.length; i++) {
5 System.out.println( nums[i] );
6 }
7}
8
1// Find-Max
2// Given a non-empty array of ints, returns
3// the largest int value found in the array.
4// (does not work with empty arrays)
5public int findMax(int[] nums) {
6 int maxSoFar = nums[0]; // use nums[0] as the max to start
7
8 // Look at every element, starting at 1
9 for (int i=1; i<nums.length; i++) {
10 if (nums[i] > maxSoFar) {
11 maxSoFar = nums[i];
12 }
13 }
14 return maxSoFar;
15}
16
1// Another way to write search that combines
2// the "end of array" and "found" logic in one
3// while loop. As a matter of style, we prefer
4// the above version that uses the standard
5// for-all loop.
6public int searchNotAsGood(int[] nums, int target) {
7 int i = 0;
8 while (i<nums.length && nums[i]!=target) {
9 i++;
10 }
11 // get here either because we found it, or hit end of array
12 if (i==nums.length) {
13 return -1;
14 }
15 else {
16 return i;
17 }
18}
19