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 :: spring boot param object 
Java :: runnable java 
Java :: java android development find element by id 
Java :: how to get internet speed in android programmatically 
Java :: how to make arraylist character 
Java :: hash table implementation java 
Java :: java repeat method 
Java :: switch case enum java 
Java :: uses or overrides a deprecated API. 
Java :: Imageview on the bottom left of Imageview android anchor 
Java :: is java compiled or interpreted 
Java :: keytool error: java.lang.Exception: Key pair not generated, alias <demo already exists 
Java :: change status bar text color android programmatically 
Java :: quarkus maven skip test 
Java :: string to int error 
Java :: java try with resources 
Java :: java how to create an array with existing objects 
Java :: first line of java code 
Java :: java character 
Java :: declare a hashmap in java 
Java :: get executable path java 
Java :: java not equal to 
Java :: insert data from database sqlite android 
Java :: binary to decimal java 
Java :: open new fragment from fragment 
Java :: java rgb to color 
Java :: multiple inheritance using interface in java 
Java :: java method parameters 
Java :: how to get orientation lock to portrait android stackoverflow 
Java :: How to get the nth Fibonacci number code in Java using recursion with memoization 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =