Linear Search
Array in Data Structure
Java array is an object which contains elements of a similar data
type. Additionally, The elements of an array are stored in a
contiguous memory location. It is a data structure where we
store similar elements. We can store only a fixed set of elements
in a Java array.
Properties of Array
There are some of the properties of an array that are listed as follows -
• Each element in an array is of the same data type and carries the same
size that is 4 bytes.
• Elements in the array are stored at contiguous memory locations from
which the first element is stored at the smallest memory location.
• Elements of the array can be randomly accessed since we can calculate
the address of each element of the array with the given base address
and the size of the data element.
Representation of Array
Why are arrays required?
• Sorting and searching a value in an array is easier.
• Arrays are best to process multiple values quickly and easily.
• Arrays are good for storing multiple values in a single variable - In
computer programming, most cases require storing a large number
of data of a similar type. To store such an amount of data, we need
to define a large number of variables. It would be very difficult to
remember the names of all the variables while writing the
programs. Instead of naming all the variables with a different
name, it is better to define an array and store all the elements into
it.
Basic Operations
Traversal - This operation is used to print the elements of the
array.
Insertion - It is used to add an element at a particular index.
Deletion - It is used to delete an element from a particular
index.
Search - It is used to search an element using the given index
or by the value.
Update - It updates an element at a particular index.
Traversal Operation
public class Traversal{
public static void main(String args[]){
int a[] = {33,3,4,5};
for(int I = 0; i < a.length; i++)
System.out.println(a[i]);
}}
Insertion Operation
import java.util.Scanner;
public class Insert_Array {
public static void main(String[] args) {
int n, pos, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n+1];
System.out.println("Enter all the elements:");
Insertion Operation
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the position where you want to insert
element:");
pos = s.nextInt();
Insertion Operation
System.out.print("Enter the element you want to insert:");
x = s.nextInt();
for(int i = (n-1); i >= (pos-1); i--)
{
a[i+1] = a[i];
}
a[pos-1] = x;
Insertion Operation
System.out.print("After inserting:");
for(int i = 0; i < n; i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n]);
}
}
Deletion Operation
import java.util.Arrays;
public class ArrayDelete {
public static void main(String[] args) {
int firstArray[] = {1,2,3,4,5};
int secondArray[] = new int[firstArray.length -1];
int j = 3;
Deletion Operation
for(int i = 0, k = 0; i <firstArray.length; i++){
if(I != j){
secondArray[k] = firstArray[i];
k++;
}
}
Deletion Operation
System.out.println("Before deletion :" +
Arrays.toString(firstArray));
System.out.println("After deletion :" +
Arrays.toString(secondArray));
}
}
What is toString() method
The toString() method returns the String representation of the
object.
If you print any object, Java compiler internally invokes
the toString() method on the object. So overriding the
toString() method, returns the desired output, it can be
the state of an object etc.
Update Operation
import java.util.*;
public class UpdateArray {
public static void main(String[] args) {
int firstArray[] = {1,2,3,4,5};
Scanner input = new Scanner(System.in);
System.out.print("Enter the new Element: ");
int update = input.nextInt();
Update Operation
System.out.print("Enter the array index: ");
Scanner indx = new Scanner(System.in);
int index = indx.nextInt();
Update Operation
System.out.println("Before Update :" +
Arrays.toString(firstArray));
firstArray[index] = update;
System.out.println("After Update :" +
Arrays.toString(firstArray));
}
}
What is Searching?
Searching is the process of finding some particular
element in the list. If the element is present in the
list, then the process is called successful, and the
process returns the location of that element;
otherwise, the search is called unsuccessful.
Types of Searching?
Two popular search methods are:
1. Linear Search
2. Binary Search
Linear Search
Linear search is also called as sequential search
algorithm. It is the simplest searching algorithm. In
Linear search, we simply traverse the list completely and
match each element of the list with the item whose
location is to be found. If the match is found, then the
location of the item is returned; otherwise, the
algorithm returns NULL.
How to implement?
The steps used in the implementation of Linear Search are listed as
follows:
1. First, we have to traverse the array elements using a for loop.
2. In each iteration of for loop, compare the search element with the
current array element, and -
• If the element matches, then return the index of the corresponding
array element.
• If the element does not match, then move to the next element.
3. If there is no match or the search element is not present in the given
array, return -1.
How to implement?
How to implement?
How to implement?
How to implement?
What is Time Complexity?
Time complexity is defined in terms of how many times it
takes to run a given algorithm, based on the length of the
input. Time complexity is not a measurement of how
much time it takes to execute a particular algorithm
because such factors as programming language, operating
system, and processing power are also considered.
What is Space Complexity?
When an algorithm is run on a computer, it necessitates a
certain amount of memory space. The amount of memory
used by a program to execute it is represented by its space
complexity. Because a program requires memory to store
input data and temporal values while running, the space
complexity is auxiliary and input space.
Implementation of Linear Search
public class LinearSearch {
static int linearSearch(int a[], int n, int val) {
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
Implementation of Linear Search
public static void main(String args[]) {
int a[] = {55, 29, 10, 40, 57, 41, 20, 24, 45};
int val = 10;
int n = a.length;
int res = linearSearch(a, n, val);
System.out.println();
Implementation of Linear Search
System.out.print("The elements of the array are - ");
for (int i = 0; i < n; i++)
System.out.print(" " + a[i]);
System.out.println();
System.out.println("Element to be searched is - " + val);
if (res == -1)
Implementation of Linear Search
System.out.println("Element is not present in the array");
else
System.out.println("Element is present at " + res +" position of
array");
}
}