Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Linear Search Java

// 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 :: calling activity method from fragment: 
Java :: compare numbers in array in java 
Java :: make textview invisible android 
Java :: number format java 
Java :: java is list ordered 
Java :: how to check palindrome in java 
Java :: get all keys from pbject javascirpt 
Java :: tomcat 9 default username and password 
Java :: java display message 
Java :: how to use user uid in android 
Java :: how to get color from a hex string in android studio 
Java :: for cicle java 
Java :: android java get value from listview item 
Java :: what does setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) is used for 
Java :: java decompiler 
Java :: assert log in unit testing 
Java :: java array sortieren 
Java :: public class in java 
Java :: how to check if user is logged in firebase android and then load another activity 
Java :: equals ignore case java 
Java :: rider find and replace 
Java :: java string.format system.currenttimemillis 
Java :: button onclick java 
Java :: breadth first search bst java 
Java :: read file from resources java 
Java :: get spring application context 
Java :: java hashmap time complexity 
Java :: initialize hashmap java 
Java :: program to check if the given date is in in the format dd/mm/yyyy java 
Java :: android java how to blur an image 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =