0% found this document useful (0 votes)
114 views8 pages

Searching

The document discusses linear search and binary search algorithms. Linear search sequentially checks each element of an array to see if it matches the target value. Binary search works on a sorted array, comparing the target to the middle element and recursively searching the appropriate half. It also provides Java code examples to implement both search algorithms. The document ends by briefly explaining different computer storage structures from cache/memory to magnetic disks, flash memory, tape storage and their characteristics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views8 pages

Searching

The document discusses linear search and binary search algorithms. Linear search sequentially checks each element of an array to see if it matches the target value. Binary search works on a sorted array, comparing the target to the middle element and recursively searching the appropriate half. It also provides Java code examples to implement both search algorithms. The document ends by briefly explaining different computer storage structures from cache/memory to magnetic disks, flash memory, tape storage and their characteristics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Searching

C.Krishna Priya,
Lecturer in Computer Science,
Sri Sai Degree College, Anantapur
Linear Search
This is a simplest search technique assuming the search is an
array. The approach is to iterate through all the elements until a match is found.
We begin search by comparing the first element of the list with the target element.
If it matches, the search ends. Otherwise, we will move to next element and
compare. That is, In this technique the target element is compared with all the
elements until a match element occurs or when there are no elements left to be
compared.

Algorithm:
linearSearch(a, n, key)
a: array
n:size of an array
key: element to be searched
step 1: i:=1;
step 2: while(i<=n) do
begin
step 3: if a[i]=key then
begin
step 4: print “target element found at position”,i
step 5: return(i);
end
step 6: i:=i+1;
end
step 7: print “unsuccessful search”
return(-1);

Program to implement Linear Search

import java.io.*;
class LinearSearch
{
public static void main(String[] args) throws Exception
{
int a[]=new int[5];
int i;
BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter 5 Elements in to the Array");
1
Searching
C.Krishna Priya,
Lecturer in Computer Science,
Sri Sai Degree College, Anantapur
for(i=0;i<a.length;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println("The elements in the array are :");
for(i=0;i<a.length;i++)
System.out.println(a[i]);
System.out.println("Enter element to search");
int key=Integer.parseInt(br.readLine());
int pos=-1;
for(i=0;i<a.length;i++)
{
if(a[i]==key)
{
pos=i;
break;
}
}

if(pos>=0)
{
System.out.println(" The element "+ key + " is found at "+ (pos+1));
}

else
System.out.println("\n The search element is not found in the array.");
}
}

Binary Search:
Suppose an array A which is sorted in increasing order then there
is an extremely efficient searching algorithm called “Binary Search”, which can
be used to find a location of a given item in an array A.

If a[mid]=item, the search is successful and set the location=mid , otherwise


when a new segment of array is obtained as follows:
 If item<a[mid] then item can appear only in the left side of mid
 If item>a[mid] then item can appear only in the right side of mid

2
Searching
C.Krishna Priya,
Lecturer in Computer Science,
Sri Sai Degree College, Anantapur
Algorithm:
binarySearch(a, n, key)
a: array
n:size of an array
key: element to be searched

Step 1: low :=1


Step 2: high :=n
Step 3: while low<=high do
Begin
Step 4: mid:=trunc((low+high)/2)
Step 5: if key=a[mid] then
Step 6: print “target element found at position “, mid;
Step 7: return mid;
end
else
Step 8: if key<a[mid] then
high:=mid-1;
else
low:=mid+1;
Step 9: print “Unsucessful Search”
Step 10: return(-1)

Program to implement Binary Search :

import java.io.*;
class BinarySearch
{
static int binarySearch(int a[],int key)
{
int low,high,mid,x;
low=0;
x=a.length;
high=x-1;
while (low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
return mid;
3
Searching
C.Krishna Priya,
Lecturer in Computer Science,
Sri Sai Degree College, Anantapur
else
if (key>a[mid])
low=mid+1;
else
if (key<a[mid])
high=mid-1;
else
System.out.println("Element Not Found\n");
}
return -1;
}
static void print(int a[])
{
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
static void sort(int a[])
{
int x,temp,i,j;
x=a.length;
for(i=0;i<x;i++)
for(j=i+1;j<x;j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
public static void main(String args[]) throws Exception
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int a[]=new int[10];
int key,pos;
System.out.println("Enter Elements into the Array\n");
for(int i=0;i<a.length;i++)
4
Searching
C.Krishna Priya,
Lecturer in Computer Science,
Sri Sai Degree College, Anantapur
a[i]=Integer.parseInt(br.readLine());
System.out.println("Enter Element to be Searched \n");
key=Integer.parseInt(br.readLine());
System.out.println("Elements in Array Before Sorting \n");
print(a);
sort(a);
System.out.println("Elements After Sorting \n");
print(a);
pos=binarySearch(a,key);
if (pos>0)
{
System.out.print(key);
System.out.println("Found at position "+(pos+1));
}
else
System.out.println("Element not Found\n");
}
}

5
Searching
C.Krishna Priya,
Lecturer in Computer Science,
Sri Sai Degree College, Anantapur
Briefly explain about storage structures?

Several types of data storage exist in most computer systems. They vary in speed
of access, cost per unit of data and reliability.
1. Cache: most costly and fastest form of storage. Usually very small, and
managed by the operating system.
2. Main Memory: the storage area for data availability to be operated on.
General -purpose machine instructions operate on main memory. Contents
of main memory are usually lost in a power failure or crash. Usually too
small and too expensive.
3. Flash Memory: EEPROM(electrically erasable programmable read-only
memory)
 Data in flash memory survive from power failure.
 Reading data from flash memory takes about 10 nano-secs and
writing data into flash memory is more complicated. Write once takes
about 4-10 microseconds.
 To overwrite what has been written, one has to first erase the entire
bank of memory. It may support only a limited number of erase
cycles( 104 to 106).
 It has found its popularity as a replacement for disks for storing small
volumes of data (5-10 megabytes).
4. Magnetic disk storage : primary medium for long-term storage.
 Typically the entire database is stored on disk
 Data must be moved from disk to main memory in order for the data
to be operated on
 After operation are performed, data must be copied back to disk if any
changes were made.
 Disk storage is called direct access storage as it is possible to read
data on the disk in any order
 Disk storage usually survives power failures and system crashes.
5. Optical storage: CD-ROM , WORM (write-once read many) disk (for
archival storage of data) and juke box (containing a few drives and
numerous disks loaded on demand).
6. Tape storage: used primarily for backup and archival data.
 Cheaper , but much slower access, since tape must be read
sequentially from the beginning.
 Used as protection from disk failures.
Another classification: primary , secondary and tertiary storage.

6
Searching
C.Krishna Priya,
Lecturer in Computer Science,
Sri Sai Degree College, Anantapur
Primary storage: the fastest storage media, such as cache and main memory.
Secondary storage: the next level of the hierarchy e.g., magnetic disks.
Tertiary or offline storage: magnetic tapes and optical disk etc .

Explain about various types of file structures?

There are three types of organizing the file:

1. Sequential access file organization


2. Direct access file organization
3. Indexed sequential access file organization
1. Sequential access file organization

Storing and sorting in contiguous block within files on tape or disk is called
as sequential access file organization. In sequential access file organization,
all records are stored in a sequential order. The records are arranged in the
ascending or descending order of a key field. Sequential file search starts from
the beginning of the file and the records can be added at the end of the file. In
sequential file, it is not possible to add a record in the middle of the file without
rewriting the file.

Advantages of sequential file


 It is simple to program and easy to design.
 Sequential file is best use if storage space.
Disadvantages of sequential file
 Sequential file is time consuming process.
 It has high data redundancy.
 Random searching is not possible.
2. Direct access file organization
Direct access file is also known as random access or relative file organization.
In direct access file, all records are stored in direct access storage device
(DASD), such as hard disk. The records are randomly placed throughout the
file. The records does not need to be in sequence because they are updated
directly and rewritten back in the same location. This file organization is useful
for immediate access to large amount of information. It is used in accessing
large databases. It is also called as hashing.
Advantages of direct access file organization
 Direct access file helps in online transaction processing system (OLTP)
like online railway reservation system.
 In direct access file, sorting of the records are not required.
 It accesses the desired records immediately.
 It updates several files quickly.
 It has better control over record allocation.

7
Searching
C.Krishna Priya,
Lecturer in Computer Science,
Sri Sai Degree College, Anantapur
Disadvantages of direct access file organization
 Direct access file does not provide backup facility.
 It is expensive.
 It has less storage space as compared to sequential file.
3. Indexed sequential access files organization
Indexed sequential access file combines both sequential file and direct access
file organization. In indexed sequential access file, records are stored randomly
on a direct access device such as magnetic disk by a primary key. This file
have multiple keys. These keys can be alphanumeric in which the records are
ordered is called primary key. The data can be access either sequentially or
randomly using the index. The index is stored in a file and read into memory
when the file is opened.
Advantages of Indexed sequential access file organization
 In indexed sequential access file, sequential file and random file access is
possible.
 It accesses the records very fast if the index table is properly organized.
 The records can be inserted in the middle of the file.
 It provides quick access for sequential and direct processing.
 It reduces the degree of the sequential search.
Disadvantages of Indexed sequential access file organization
 Indexed sequential access file requires unique keys and periodic
reorganization.
 Indexed sequential access file takes longer time to search the index for
the data access or retrieval.
 It requires more storage space.
 It is expensive because it requires special software.
 It is less efficient in the use of storage space as compared to other file
organizations.

You might also like