This is my implementation of finding a peak both in 1-dimensional and 2-dimensional arrays.
Note that in Java, 2D matrices are indexed by rows so I changed the algorithm to process rows instead of columns (differently from the MIT lesson).
The MIT algorithm was this:
- Pick the middle column
- Find the global max on column j at (i,j)
- Compare (i,j-1), (i,j), (i,j+1)
- Pick left cols if (i,j-1) > (i,j)
- Pick right cols if (i,j+1) > (i,j)