Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java Linear Search Algorithm

// Java Linear Search Algorithm
// ----------------------------

/* 
   Time Complexity
     Best Time Complexity:O(1)
	 Average Time Complexity:O(n)
	 Worst Time Complexity:O(n)
     
   Space Complexity
     No auxiliary space is required in Linear Search implementation.
	 Hence space complexity is:O(1)
*/

class LinearSearch
{
    public static int search(int arr[], int x)
    {
        int n = arr.length;
        for (int i = 0; i < n; i++)
        {
            if (arr[i] == x)
                return i;
        }
        return -1;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 2, 3, 4, 10, 40 };
        int x = 10;
 
        // Function call
        int result = search(arr, x);
        if (result == -1)
            System.out.print(
                "Element is not present in array");
        else
            System.out.print("Element is present at index "
                             + result);
    }
}
Comment

linear search algorithm java

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.SortingAlgorithm;

import java.util.Scanner;

public class LinearSearch {
    public LinearSearch() {
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of Element you want: ");
        int n = sc.nextInt();
        int[] arr = new int[n];
        System.out.println("Enter " + n + " values");

        int target;
        for(target = 0; target < n; ++target) {
            arr[target] = sc.nextInt();
        }

        System.out.println("Enter the value of target Element: ");
        target = sc.nextInt();

        for(int i = 0; i < n; ++i) {
            if (arr[i] == target) {
                System.out.println("Element found at index: " + i);
                break;
            }

            System.out.println("Element not found at index: " + i);
        }

    }
}
Comment

linear serach in java

Step 1: Traverse the array
Step 2: Match the key element with array element
Step 3: If key element is found, return the index position of the array element
Step 4: If key element is not found, return -1
Comment

PREVIOUS NEXT
Code Example
Java :: retrofit post header 
Java :: final finally finalize 
Java :: String remove duplicate method in java 
Java :: java.lang.arrayindexoutofboundsexception: index 3 out of bounds for length 3 
Java :: importing all java libraries 
Java :: palindrome find in java 
Java :: retrieving parent element from child aWebelement selenium java 
Java :: java make a null string 
Java :: stringbuffer in java 
Java :: get index of element java 
Java :: how to substring in java 
Java :: java fot 
Java :: antialiasing kjava 
Java :: unable to access jarfile 
Java :: java stringbuilder 
Java :: java print default options 
Java :: how junit test getter and setter 
Java :: what coding language is minecraft 
Java :: java string get ith char 
Java :: socket in java 
Java :: java program to Check given String is contians number or not 
Java :: java http response code 
Java :: jdk 15 download brew 
Java :: how to create folder java 
Java :: list.of java 
Java :: actuator spring boot 
Java :: string palindrome in java 
Java :: type of exception in java 
Java :: Java Access ArrayList Elements 
Java :: java character for end of file 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =