A. Introduction
In _7_1.java, we just finds the maximum. How can we find maximum or minimum if we have 100 values? Discuss what the output of class _7_2 is and why.
class _7_2 {
public static int max(int[] x ){
int m = x[0];
for(int i=1;i<x.length;i++){
if(m<=x[i]) m=x[i];
}
return m;
}
public static void max2(int[] x ){
int m = x[0];
for(int i=1;i<x.length;i++){
if(m<=x[i]) m=x[i];
}
x[0]= m;
}
public static void main(String[] args) {
int[] arr = {32, 12, 45, -90, -67, 55, 88, 123, 765, 909};
System.out.println(max(arr));
max2(arr);
System.out.println(arr[0]);
}
}
B. Notes:
B) Furthermore, if we can find maximum or minimum of an array, how do we sort an array?
In general, there are many algorithms to sort arrays, Selection sort and bubble sort are among the easiest.
C) In addition, searching arrays for certain values, reversing arrays are also common operations.
C. HW: Complete 1) a bubble sort method, 2) a searching method, and 3) a reverse method in _7_2.java. Details are in code comments