Searching and Sorting Techniques
LINEAR SEARCH
WATCH THE VIDEO https://www.youtube.com/watch?v=NG7M_SJqJhM&t=278s
1A ) Linear Search or Sequential Search JAVA PROGRAM
public class LinearSearch
public int linearSearch(int[] arr, int key)
for (int i = 0; i < arr.length; i++)
if (arr[i] == key)
return i; // Return index if element is found
return -1; // Return -1 if element is not found
public void printArray(int[] arr)
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
System.out.println();
public static void main(String[] args)
int [] arr = {64, 25, 12, 22, 11};
LinearSearch ob = new LinearSearch();
System.out.println("Original array:");
ob.printArray(arr);
int key = 22;
int index = ob.linearSearch(arr, key);
if (index != -1)
System.out.println("Element " + key + " found at index: " + index);
else
System.out.println("Element " + key + " not found in the array.");
}
1B ) Linear Search or Sequential Search PSEUDOCODE
1C )Linear Search or Sequential Search USING RECURSION
// Java Recursive Code For Linear Search
import java.io.*;
class Test
{
// Recursive Method to search key in the array
static int linearsearch(int arr[], int size, int key)
{
if (size == 0)
{
return -1;
}
else if (arr[size - 1] == key)
{
// Return the index of found key.
return size - 1;
}
return linearsearch(arr, size - 1, key);
}
// Driver method
public static void main(String[] args)
{
int arr[] = { 5, 15, 6, 9, 4 };
int key = 4;
// Function call to find key
int index = linearsearch(arr, arr.length, key);
if (index != -1)
System.out.println(
"The element " + key + " is found at "
+ index + " index of the given array.");
else
System.out.println("The element " + key
+ " is not found.");
}
}
BINARY SEARCH
WATCH THE VIDEO https://www.youtube.com/watch?v=LuuI1j_g7fk&t=2s
The precondition to using Binary Search is that the array should be
sorted.
2A ) Binary Search JAVA PROGRAM
import java.io.*;
public class BinarySearch
{
int binarySearch(int arr[], int key)
{
int first = 0
int last = arr.length - 1;
while (first <= last)
{
int mid = (first + last )/ 2;
if (arr[mid] == key)
return mid;
if (arr[mid] < key)
first = mid + 1;
else
last = mid - 1;
}
return -1;
}
void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String args[])
{
int arr[] = {12,22,45,67,76,83};
BinarySearch bs = new BinarySearch();
int key = 22; // Element to search
int result = bs.binarySearch(arr, key);
if (result != -1)
System.out.println("Element " + key + " found at index " + result);
else
System.out.println("Element " + key + " not found");
}
}
2B) Binary Search PSEUDOCODE
2C) Binary Search Recursion
// Java implementation of iterative Binary Search
import java.io.*;
class BinarySearch
{
// Returns index of x if it is present in arr[].
int binarySearch(int arr[], int x)
{
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Check if x is present at mid
if (arr[mid] == x)
return mid;
// If x greater, ignore left half
if (arr[mid] < x)
low = mid + 1;
// If x is smaller, ignore right half
else
high = mid - 1;
}
// If we reach here, then element was
// not present
return -1;
}
// Driver code
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, x);
if (result == -1)
System.out.println( "Element is not present in array");
else
System.out.println("Element is present at " + "index " + result);
}
}