Skip to main content

✅ Day 3: Searching Algorithms in Java


🎯 Goals:

  • Understand Linear Search and Binary Search

  • Learn when and how to use each

  • Solve real interview problems using searching techniques


🔍 1. Linear Search

📘 Definition:

Linearly checks each element one by one. Used when the array is unsorted.

✅ Code Example:


public static int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; } return -1; // Not found }

🧠 2. Binary Search

📘 Definition:

Used on sorted arrays. It divides the search space in half each time — O(log N) time complexity.

✅ Code (Iterative Approach):


public static int binarySearch(int[] arr, int key) { int start = 0, end = arr.length - 1; while (start <= end) { int mid = start + (end - start) / 2; if (arr[mid] == key) return mid; else if (arr[mid] < key) start = mid + 1; else end = mid - 1; } return -1; }

✅ Code (Recursive Approach):


public static int binarySearchRec(int[] arr, int key, int start, int end) { if (start > end) return -1; int mid = start + (end - start) / 2; if (arr[mid] == key) return mid; else if (arr[mid] < key) return binarySearchRec(arr, key, mid + 1, end); else return binarySearchRec(arr, key, start, mid - 1); }

💡 3. Binary Search Use Cases

  • Search in sorted array

  • Find first/last occurrence of an element

  • Search in rotated sorted arrays

  • Peak elements in mountain arrays


✍️ 4. Practice Problems

🔹 Write functions to:

  • Find first and last occurrence of an element (e.g. in sorted duplicates)

  • Count how many times a number appears in sorted array

  • Search in a rotated sorted array

  • Find square root using binary search (approximate to floor)


🔥 5. BONUS – Search in 2D Matrix

🔹 Use binary search logic on 2D:


public static boolean searchMatrix(int[][] matrix, int target) { int rows = matrix.length; int cols = matrix[0].length; int low = 0, high = rows * cols - 1; while (low <= high) { int mid = low + (high - low) / 2; int value = matrix[mid / cols][mid % cols]; if (value == target) return true; else if (value < target) low = mid + 1; else high = mid - 1; } return false; }

📚 Homework for Day 3:


⏭️ What’s Coming on Day 4?

  • Sorting Algorithms: Bubble, Selection, Insertion (with time/space analysis)


Comments

Popular posts from this blog

Raster scan Vs Vector Scan

1. Raster Scan Display   How It Works : A raster scan display works by painting an image on the screen pixel by pixel, row by row. It follows a systematic pattern where the electron beam (in CRT monitors) or the display elements (in modern LCD/LED screens) sweep across the screen from left to right, top to bottom, in a series of horizontal lines (scan lines). This process is akin to how a traditional TV screen works.   Process : The display draws the image starting from the top-left corner, moving to the right, then moves to the next row below, and repeats this process until the entire screen is filled. This pattern creates a grid of pixels, where each pixel can have a color and brightness level.   Characteristics : Pixel-based : The screen consists of a grid of pixels, and each pixel can have a distinct color and intensity. Continuous Image : Raster scan displays are capable of displaying detailed and complex images, including photographs and videos, because they break t...

Inheritance

*■ Inheritance*  • Inheritance is a concept in OOP that allows a class to inherit properties and behaviors (methods) from another class. • A class that inherits from another class is called a derived class (or subclass) • The class which gets inherited by another class is called the base class (or superclass). • Inheritance is possible only if there is is-a relationship between parent and child class. • constructors are not inherited in derived class, however the derived class can call default constructor implicitly and if there's a parameterised constructors in bass class then derived class can call it using 'base' keyword.  ____________________________________________  *➤ Rules of Inheritance*  1) C# supports single inheritance, meaning a class can inherit from only one base class. 2) A parent class constructor must be accessible in child class otherwise  inheritance will be not possible. 3) every class, whether user-defined or predefined implicitly derives fr...

unit -1 Introduction of Image processing

  What is Image Processing? Image processing is a method to perform operations on an image to enhance it or extract useful information. It is a type of signal processing where the input is an image, and the output may be either an image or characteristics/features associated with that image. Goals of Image Processing Image Enhancement : Improving visual appearance (e.g., contrast, sharpness) Image Restoration : Removing noise or distortion Image Compression : Reducing the amount of data required to represent an image Feature Extraction : Identifying objects, edges, or patterns Image Analysis : Understanding and interpreting image content Object Recognition : Detecting and identifying objects in an image What is an Image? An image is a two-dimensional function f(x, y) , where x and y are spatial coordinates, and f is the intensity (brightness or color) at that point. For digital images, both x, y, and f are finite and discrete. Types of Image Representation...