{"id":1842,"date":"2019-04-04T18:25:10","date_gmt":"2019-04-04T18:25:10","guid":{"rendered":"https:\/\/andnewsindia.com\/?p=1842"},"modified":"2019-04-04T18:25:10","modified_gmt":"2019-04-04T18:25:10","slug":"difference-between-linear-search-and-binary-search","status":"publish","type":"post","link":"https:\/\/andnewsindia.com\/difference-between-linear-search-and-binary-search\/","title":{"rendered":"Difference Between Linear Search and Binary Search"},"content":{"rendered":"

Difference Between Linear Search and Binary Search<\/h1>\n

Difference Between Linear Search and Binary Search<\/strong>: When it comes to the search operation in programming, two names come in mind. Linear search and binary search. Some people confuse in these terms. So let\u2019s see what are the difference between liner search and binary search?<\/p>\n

What is Linear search?<\/h2>\n

Linear search is a simple searching algorithm. In this type of search, the searching process occurs from one item after the other. Means this search algorithm checks every item present and checks for a matching item on that.<\/p>\n

Searching starts from the first item. If the item does not match, then it moves to the second item and does the same. It continues the same process until it finds the match.<\/p>\n

In a linear search, the efficiency of the algorithm determined by the number of comparisons to search an element or time consumption in the process. If the element we are searching for, is in the first position of the data structure, then it requires only one comparison. In this case, the efficiency will be good because it takes less time to find the item. When the element we are searching for, is in the last position, then it requires an N number (number of data items) of comparisons to find the element.<\/p>\n

The pseudocode of linear search algorithms look like this \u2013<\/p>\n

procedure linear_search (list, value)<\/p>\n

 <\/p>\n

for each item in the list<\/p>\n

if match item == value<\/p>\n

return the item’s location<\/p>\n

end if<\/p>\n

end for<\/p>\n

end procedure<\/p>\n

Also, read – DDR3 vs DDR3L and DDR4 vs DDR4L | Difference between DDR3 and DDR3L<\/a><\/p>\n

What is a Binary search?<\/h2>\n

The binary search algorithm is faster as compared to a linear search. To perform this search, the data collection should be in the sorted form. The search algorithm finds the item by start comparing from the middle most item of the collection.<\/p>\n

To determine the middle of any collection, it uses a formula.<\/p>\n

Mid = low + (high – low) \/ 2<\/p>\n

Low and high refers to the position number (Like 0, 1, 2, 3, 4 etc) which starts from the left.<\/p>\n

If a match occurs in the middle position, then the index of the item is returned. If the middle item is greater than the searched item, then it searches in the sub-array to the left of the middle item. Otherwise, it searches in the sub-array to the right of the middle item. With each search, the number of items to search decreases each time. This same process continues on the sub-array then sub-array, until the size of the sub-array reduces to zero.<\/p>\n

The pseudocode of binary search algorithms look like this \u2013<\/p>\n

Procedure binary_search<\/p>\n

A \u2190 sorted array<\/p>\n

n \u2190 size of an array<\/p>\n

x \u2190 value to be searched<\/p>\n

 <\/p>\n

Set lowerBound = 1<\/p>\n

Set upperBound = n<\/p>\n

 <\/p>\n

while x not found<\/p>\n

if upperBound < lowerBound<\/p>\n

EXIT: x does not exist.<\/p>\n

 <\/p>\n

set midPoint = lowerBound + ( upperBound – lowerBound ) \/ 2<\/p>\n

 <\/p>\n

if A[midPoint] < x<\/p>\n

set lowerBound = midPoint + 1<\/p>\n

 <\/p>\n

if A[midPoint] > x<\/p>\n

set upperBound = midPoint – 1<\/p>\n

 <\/p>\n

if A[midPoint] = x<\/p>\n

EXIT: x found at location midPoint<\/p>\n

end while<\/p>\n

end procedure<\/p>\n

Also, read – Computer Full Form | Computer Related Full Form<\/a><\/p>\n

Difference between linear search and binary search<\/h2>\n

As of now, we have seen some of the basic things related to both of these searches. Based on that you may differentiate some points as well. But let\u2019s try to grab most and see what are the major difference between linear search and binary search.<\/p>\n\n\n\n\n\n\n\n\n\n\n\n
\n

Linear search<\/strong><\/p>\n<\/td>\n

\n

Binary search<\/strong><\/p>\n<\/td>\n<\/tr>\n

\n

In a linear search, it scans one item at a time in a sequential basis without jumping to any item<\/p>\n<\/td>\n

Binary search divides the whole array to half and starts searching. If not found then it searches in the left or right sub-array<\/td>\n<\/tr>\n
\n

Input data need not to be sorted in linear search<\/p>\n<\/td>\n

\n

Input data needs to be sorted in before starting the binary search<\/p>\n<\/td>\n<\/tr>\n

\n

Also called sequential search<\/p>\n<\/td>\n

\n

Also called logarithmic search or half-interval search<\/p>\n<\/td>\n<\/tr>\n

\n

Time complexity is O(N)<\/p>\n<\/td>\n

\n

Time complexity is O(log2N)<\/p>\n<\/td>\n<\/tr>\n

\n

Best case is to find the element in the first position<\/p>\n<\/td>\n

\n

Best case is to find the element in the middle position<\/p>\n<\/td>\n<\/tr>\n

\n

Take more time<\/p>\n<\/td>\n

\n

Take less time<\/p>\n<\/td>\n<\/tr>\n

\n

Less efficient<\/p>\n<\/td>\n

\n

More efficient<\/p>\n<\/td>\n<\/tr>\n

Less complex<\/td>\n\n

More complex<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

 <\/p>\n

Conclusion<\/h3>\n

Linear search and binary search both are used to search an element in a data structure such as an array. Of course, binary search is more efficient than the linear search but the elements should be shorted in the array. These are some of the difference between linear search and binary search you may consider. Depending on the requirements, you can choose one of them.<\/p>\n","protected":false},"excerpt":{"rendered":"

Difference Between Linear Search and Binary Search Difference Between Linear Search and Binary Search: When it comes to the search operation in programming, two names come in mind. Linear search and binary search. Some people confuse in these terms. So let\u2019s see what are the difference between liner search and binary search? What is Linear […]<\/p>\n","protected":false},"author":1,"featured_media":1844,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[123,389,399],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/posts\/1842"}],"collection":[{"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/comments?post=1842"}],"version-history":[{"count":0,"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/posts\/1842\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/media\/1844"}],"wp:attachment":[{"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/media?parent=1842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/categories?post=1842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andnewsindia.com\/wp-json\/wp\/v2\/tags?post=1842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}