JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: Write a program to implement Linear search or Sequential search algorithm.


Linear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.

Linear search is the simplest search algorithm. For a list with n items, the best case is when the value is equal to the first element of the list, in which case only one comparison is needed. The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed.

The worst case performance scenario for a linear search is that it has to loop through the entire collection, either because the item is the last one, or because the item is not found. In other words, if you have N items in your collection, the worst case scenario to find an item is N iterations. In Big O Notation it is O(N). The speed of search grows linearly with the number of items within your collection.

Linear searches don't require the collection to be sorted.


package com.java2novice.algos;

	public class MyLinearSearch {

	public static int linerSearch(int[] arr, int key){
		
		int size = arr.length;
		for(int i=0;i<size;i++){
			if(arr[i] == key){
				return i;
			}
		}
		return -1;
	}
	
	public static void main(String a[]){
		
		int[] arr1= {23,45,21,55,234,1,34,90};
		int searchKey = 34;
		System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr1, searchKey));
		int[] arr2= {123,445,421,595,2134,41,304,190};
		searchKey = 421;
		System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr2, searchKey));
	}
}


Output:
Key 34 found at index: 6
Key 421 found at index: 2
 Next Program >>
blog comments powered by Disqus

Java Search Algorithms Examples

  1. Write a program to implement Linear search or Sequential search algorithm.
  2. Implement Binary search in java using divide and conquer technique.
  3. Implement Binary search in java using recursive algorithm.
Knowledge Centre
Preemptive scheduling Vs Time slicing?
Preemptive scheduling: The highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.

Time slicing: A task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.
Famous Quotations
Insanity: doing the same thing over and over again and expecting different results.
-- Albert Einstein

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.