Java Academy · Lesson

Searching, Min/Max & Reverse

Implement linear search, compute min/max in one pass, and reverse arrays in-place or by copy.

Lesson 2 of 38 steps

Searching, Min/Max & Reverse is a free Java Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.

Linear Search

Linear search checks each element in order until it finds the target.

  • Return the index when found
  • If not found, return -1
  • Stop early with break
public class Main {
  public static void main(String[] args) {
    // Example array
    int[] a = {5, 8, 9, 12, 15};

    int t = 9;      // target value to search
    int idx = -1;   // will stay -1 if not found

    // Loop through each index of the array
    for (int i = 0; i < a.length; i = i + 1) {
      // Check if current element matches the target
      if (a[i] == t) {
        idx = i;   // store the index where found
        break;     // exit loop early (found the target)
      }
    }

    // Print result
    if (idx != -1) {
      System.out.println("Found " + t + " at index " + idx);
    } else {
      System.out.println(t + " not found in array");
    }
  }
}

findIndex()

Encapsulate search in a method:

public class Main {
  public static void main(String[] args) {
    // Example array
    int[] a = {5, 8, 9, 12, 15};

    int t = 9;      // target value we are searching for
    int idx = -1;   // default index (-1 means "not found")

    // Loop through each index of the array
    for (int i = 0; i < a.length; i = i + 1) {
      // Check if current element matches the target
      if (a[i] == t) {
        idx = i;   // store the index where it was found
        break;     // stop searching (first match found)
      }
    }

    // Print result
    if (idx != -1) {
      System.out.println("Found " + t + " at index " + idx);
    } else {
      System.out.println(t + " not found in array");
    }
  }
}

Min/Max One-Pass

Compute min and max in one pass:

public class Main {
  public static void main(String[] args) {
    // Example array
    int[] a = {5, 8, 2, 12, 15, 1};

    // Initialize min and max with the first element
    int min = a[0];
    int max = a[0];

    // Loop starts from the second element (index 1)
    for (int i = 1; i < a.length; i = i + 1) {
      // If current element is smaller than current min → update min
      if (a[i] < min) min = a[i];

      // If current element is larger than current max → update max
      if (a[i] > max) max = a[i];
    }

    // Print results
    System.out.println("Minimum value = " + min);
    System.out.println("Maximum value = " + max);
  }
}

Reverse In-Place

Reverse in-place by swapping ends moving inward:

public class Main {
  public static void main(String[] args) {
    // Example array
    int[] a = {5, 8, 9, 12, 15};

    int i = 0;              // start pointer (left side)
    int j = a.length - 1;   // end pointer (right side)

    // Keep swapping until the two pointers meet
    while (i < j) {
      // Swap elements at positions i and j
      int tmp = a[i];
      a[i] = a[j];
      a[j] = tmp;

      // Move pointers toward the center
      i = i + 1;
      j = j - 1;
    }

    // Print the reversed array
    System.out.print("Reversed array: ");
    for (int k = 0; k < a.length; k++) {
      System.out.print(a[k] + " ");
    }
  }
}

Reversed Copy

Create a reversed copy (original unchanged):

public class Main {
  public static void main(String[] args) {
    // Example array
    int[] a = {5, 8, 9, 12, 15};

    // Create a new array b with the same length
    int[] b = new int[a.length];

    // Copy elements from a into b in reverse order
    for (int i = 0; i < a.length; i = i + 1) {
      // (a.length - 1 - i) gives the reversed index
      b[a.length - 1 - i] = a[i];
    }

    // Print original array
    System.out.print("Original array: ");
    for (int i = 0; i < a.length; i++) {
      System.out.print(a[i] + " ");
    }

    System.out.println(); // newline

    // Print reversed array
    System.out.print("Reversed array: ");
    for (int i = 0; i < b.length; i++) {
      System.out.print(b[i] + " ");
    }
  }
}

Search/MinMax/Reverse Demo

Run it: See search results, min/max values, and reversed arrays.

public class Main {
  static int findIndex(int[] a, int t) {
    for (int i = 0; i < a.length; i = i + 1) {
      if (a[i] == t) return i;
    }
    return -1;
  }

  static int min(int[] a) {
    int m = a[0];
    for (int i = 1; i < a.length; i = i + 1) {
      if (a[i] < m) m = a[i];
    }
    return m;
  }

  static int max(int[] a) {
    int m = a[0];
    for (int i = 1; i < a.length; i = i + 1) {
      if (a[i] > m) m = a[i];
    }
    return m;
  }

  static void reverseInPlace(int[] a) {
    int i = 0, j = a.length - 1;
    while (i < j) {
      int tmp = a[i];
      a[i] = a[j];
      a[j] = tmp;
      i = i + 1;
      j = j - 1;
    }
  }

  static int[] reversedCopy(int[] a) {
    int[] b = new int[a.length];
    for (int i = 0; i < a.length; i = i + 1) {
      b[a.length - 1 - i] = a[i];
    }
    return b;
  }

  static String join(int[] a) {
    String s = "";
    for (int i = 0; i < a.length; i = i + 1) {
      s = s + a[i] + (i + 1 < a.length ? " " : "");
    }
    return s;
  }

  public static void main(String[] args) {
    int[] a = {3, 1, 4, 1, 5};

    System.out.println("findIndex 4 -> " + findIndex(a, 4));
    System.out.println("findIndex 9 -> " + findIndex(a, 9));

    System.out.println("min = " + min(a));
    System.out.println("max = " + max(a));

    int[] b = reversedCopy(a);
    System.out.println("reversedCopy: " + join(b));

    reverseInPlace(a);
    System.out.println("reverseInPlace: " + join(a));
  }
}

Linear Search Check

Quick check: Which snippet returns the index of t in a or -1?

Recap & Next

Recap: You implemented linear search, min/max, and two ways to reverse arrays.

Next: Learn insertion, deletion (by index), and shifting elements.

Free to start

Learn Java with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
104
Lessons
374

Frequently Asked Questions

Is the “Searching, Min/Max & Reverse” lesson free?

Yes — the full text of “Searching, Min/Max & Reverse” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.

What will I learn in “Searching, Min/Max & Reverse”?

Implement linear search, compute min/max in one pass, and reverse arrays in-place or by copy. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Searching, Min/Max & Reverse” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Array Basics & Iteration
  2. Searching, Min/Max & Reverse
  3. Insert, Delete by Index & Shift
← Back to Java Academy