0% found this document useful (0 votes)
58 views4 pages

Two Way Linear Search Algorithm: International Journal of Computer Applications December 2014

The document describes a two way linear search algorithm that searches an array from both ends to find a target value. It initializes index variables p and q to the start and end of the array. It iterates through the array comparing elements at p and q to the target, incrementing p and decrementing q on each pass until the indexes meet. If a match is found, it returns 1, otherwise it returns -1. The algorithm is proven to always terminate in a finite number of steps as the indexes converge. It is shown to produce the correct output and compared to standard linear search, demonstrating faster performance on certain inputs.

Uploaded by

Meghraj Ranaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

Two Way Linear Search Algorithm: International Journal of Computer Applications December 2014

The document describes a two way linear search algorithm that searches an array from both ends to find a target value. It initializes index variables p and q to the start and end of the array. It iterates through the array comparing elements at p and q to the target, incrementing p and decrementing q on each pass until the indexes meet. If a match is found, it returns 1, otherwise it returns -1. The algorithm is proven to always terminate in a finite number of steps as the indexes converge. It is shown to produce the correct output and compared to standard linear search, demonstrating faster performance on certain inputs.

Uploaded by

Meghraj Ranaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/284494241

Two way Linear Search Algorithm

Article in International Journal of Computer Applications · December 2014


DOI: 10.5120/19137-9622

CITATIONS READS

11 7,156

3 authors, including:

Nitin Arora
Indian Institute of Technology Roorkee
63 PUBLICATIONS 179 CITATIONS

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Content based Image retrieval View project

An Efficient System for Early Diagnosis of Breast Cancer using Support Vector Machine View project

All content following this page was uploaded by Nitin Arora on 20 February 2019.

The user has requested enhancement of the downloaded file.


International Journal of Computer Applications (0975 – 8887)
Volume 107 – No. 21, December 2014

Two way Linear Search Algorithm

Nitin Arora Garima Bhasin Neha Sharma


Women Institute of Women Institute of Women Institute of
Technology, Dehradun Technology, Dehradun Technology, Dehradun

ABSTRACT the array and if the element has been found, if will display the
Linear search is the basic search algorithm used in data particular element and the index value of that element in that
structures. If is also called as sequential search. Linear search array[4].
is used to find a particular element in an array. It is not If we declare an array of size 8 and we initialize the array with
compulsory to arrange an array in any order (Ascending or the values (either static or dynamic declaration of values). In
Descending) as in the case of binary search. Linear search that if we mention the particular element to be identified, then
starts by sequentially scanning the elements in the array and if the two way linear search will start to search the array from
the element has been found, if will display the particular the index 0. If the value is found at its first position itself, then
element and the index value of that element in that array. In it will return the value. If the value is not found, the search
this paper we present a novel searching algorithm Two Way will increment the index value by 1 and then check the
Linear Search, which modified version of linear search element. This process continues till the element has been
algorithm and is based on comparing the elements from both identified.
ends.We have also compared the Two Way Linear search
algorithm with Linear Search Algorithm. We used MATLAB Algorithm LinearSearch(x, L, n)
8.0 for implementation and Analysis of CPU time taken by Input: Array L, value x, and number n of elements in L
both the algorithms. We have checked the algorithms
with input sequences of lengths10000, 50000, 100000, Output: Position i, 0 ≤ i < n such that L[i] = x, or −1 if x does
and 5000000.Result shows that Two Way Searching not belongs to L
Algorithm is working well for all input values and it takes
1. i←0
lesser time if the element to be search is after the middle of
the array, otherwise it takes same time as in case of linear 2. while (i < n) and (L[i] 6= x)
search. 3. do
4. i←i + 1 //end while
Keywords 5. if i < n thenreturn i
Searching, Algorithms, Linear Search, Two Way Linear 6. elsereturn -1
Search.
3. TWO WAY LINEAR SEARCH
1. INTRODUCTION ALGORITHM
An iterator traverses sequentially the array from left to right. In Two Way Linear Search we initialize the array with the
When it encounters an element which is equal to the key the values (either static or dynamic declaration of values). In that
search stops and the index of that element is returned. If the if we mention the particular element to be identified, then the
key is not present in the array the size of the array will be two way linear search will start to search the array from the
returned. Linear search is the basic search algorithm used in index 0 as well as from the index n-1. If the value is found at
data structures [1]. If is also called as sequential search. its position from the left or from the right, then it will return 1.
Linear search is used to find a particular element in an array. If the values is not found the value returned is -1.
It is not compulsory to arrange an array in any order
(Ascending or Descending) as in the case of binary search. Algorithm TwoWayLinearSearch(A, num, n)
Linear search starts by sequentially scanning the elements in Input: Array A, value num to be search and n total number n
the array and if the element has been found, if will display the elements in A
particular element and the index value of that element in that
array[2][3]. Output: 1 if num belongs to A, or -1 if num does not belongs
A
Rest of the paper is organized as follows: section 2 describes
the Related work in this we have discussed Linear search 1. p ← 0;
algorithm. Section 3 describes our new Two Way Linear 2. q ← n-1;
Search algorithm. Proof of correctness for Two Way Linear
Search is described in section 4 and comparison between 3. a ← 0;
Linear Search and Two Way Linear Search is described in
4. while (p<q)
section 5. Followed by conclusion and future scope in section
6, acknowledgement in section 7 and used references are 5. do
described in section 8.
6. if (L[p] ≠ x or L[q] ≠ x)
2. RELATED WORK 7. then p ← p + 1; q ← q - 1; a ← 0;
Linear search is used to find a particular element in an array.
It is not compulsory to arrange an array in any order 8. else
(Ascending or Descending) as in the case of binary search.
9. then a ← 1; //end while;
Linear search starts by sequentially scanning the elements in

6
International Journal of Computer Applications (0975 – 8887)
Volume 107 – No. 21, December 2014

10. if a ==1 return 1 4.2 Algorithm TwoWayLinearSearch


11. elsereturn -1 produces the correct output
In the second to last line the algorithm returns the value 1.
4. PROOF OF CORRECTNESS FOR Note that this is the correctvalue to return since as the while
TWO WAY LINEAR SEARCH loop ended and a =1, this implies that A[ ] = num.
To prove that an algorithm is correct, we need to show two
things: (1) that the algorithm terminates, and (2) that it 5. PERFORMANCE ANALYSIS AND
produces the correct output[5][6] COMPARISON
Both the searching algorithms (Linear Search and Two Way
4.1 Algorithm TwoWayLinearSearch Linear Search) were implemented in MATLAB 8.0 and
terminates after a finite number of steps tested for the input of length 10000,50000, 100000, and
The variable p takes value zero and the variable q takes the 5000000. Both the searching algorithms were executed on
values n-1, andafter each iterationof the while loop the value machine with 32-bit Operating System having Intel(R)
of p increases by 1 and the value of q decreases by 1. If the Pentium (R) CPU P6200 @ 2.13 GHz, 2.13 GHz and
while loopperformed an infinite number of iterations, installed memory (RAM) 3.00GB. The values of length of
however, this is not possible as one of the conditions of the input and CPU time taken (msec) is shown in Table 1.
while loop p < q.Thus the while loop will terminate as soon as Result shows that new Two Way Searching Algorithm is
p = q. working well for all input values and it takes lesser time if the
element to be search is after the middle of the array, otherwise
it takes same time as in case of linear search.
Table 1. CPU Time (msec) for different lengths of input sequences
10000 50000 100000 500000 1000000
Searching Element Element Element Element Element Element Element Element Element Element
Algorith present present present present present present present present present present
m at at at at at at at at at at
/Input starting ending starting ending starting ending starting ending starting ending
position position position position position position position position position position
Linear 0 0 0.0156 0.0160 0.0312 0.0614 0.0925 0.1125 0.1526 0.1714
Search
Two Way 0 0 0.0156 0 0.0312 0 0.0925 0.0154 0.1526 0.0317
Linear
Search

6. CONCLUSION AND FUTURE SCOPE [4] Frank M.C. (2004) Data Abstraction and Problem
Searching is a technique that is used to find a particular Solving with C++. US: Pearson Education, Inc.
element in an array. In Linear Search it is not compulsory to [5] Cormen T.H., Leiserson C.E., Rivest R.L. and Stein C.
arrange an array in any order (Ascending or Descending) as in (2003) Introduction to Algorithms MIT Press,
the case of binary search orderly. Result shows that new Two Cambridge, MA, 2nd edition.
Way Searching Algorithm is working well for all input values
and it takes lesser time if the element to be search is after the [6] Seymour Lipschutz (2009) Data Structure with C,
middle of the array, otherwise it takes same time as in case of Schaum Series, Tata McGraw-Hill Education.
linear search.
9. ABOUT THE AUTHORS
7. ACKNOWLEDGEMENT Nitin Arora received B. Tech. (Computer Science &
This research paper is made possible through the help and Engineering) from MNNIT Allahabad in 2008 and M. Tech.
support from everyone, including: Dr. Sandeep Goel (Computer Science & Engineering) from Govind Ballabh Pant
(Director WIT), all the faculty members of Computer Science Engineering College, Pauri, Garhwal, Uttarakhand in 2012.
& Engineering Department, all the students and friends. He is the member of IANEG (USA), ISOC (USA) and
Especially, we would like to give extremely thanks to our IACSIT (USA) and has published over several research
family because without their support this work was not papers in National and International journals/conferences
possible. in the field of Mobile Ad-Hoc Networks, Data Structures
and Algorithms. He started his career as a Lecturer from
8. REFERENCES Shobhit Institute of Engineering and Technology,
[1] Arora, N., Tamta, V., and Kumar S. 2012. A Novel Saharanpur and later on promoted as an Assistant
Sorting Algorithm and Comparison with Bubble Sort and Professor in the Department of Computer Science &
Selection Sort. International Journal of Computer Engineering at Women Institute of Technology (WIT)
Applications. Vol 45. No 1. 31-32 Constituent college of Uttarakhand Technical University
(UTU), Dehradun. He qualified National Eligibility Test
[2] Herbert Schildt Tata McGraw-Hill [2005], “The (NET) in Computer & Application in June – 2012.
Complete Reference C fourth Edition”.
Garima Bhasin received Diploma in Computer Science &
[3] Alfred V., Aho J., Horroroft, Jeffrey D.U. (2002) Data Engineering from Government Polytechnic Dehradun and
Structures and Algorithms. currently pursuing B. Tech in Computer Science &
Engineering from Women Institute of Technology, a

7
International Journal of Computer Applications (0975 – 8887)
Volume 107 – No. 21, December 2014

Constituent College of Uttarakhand Technical University, currently pursuing B. Tech in Computer Science &
Dehradun. Her area of interest includes C, C++, .Net, Java, Engineering from Women Institute of Technology, a
Database and Data structures and Algorithms. constituent Institute of Uttarakhand Technical University,
Dehradun. Her area of interest includes Computer
Neha Sharma received Diploma in Computer Science & Networking, .NET and Java.
Engineering from Government Polytechnic Dehradun and

IJCATM : www.ijcaonline.org
8

View publication stats

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy