answersLogoWhite

0


Best Answer
A linear search is effective when the order of elements in an array are not sorted by the value you are looking for. A binary search is effective when the order of elements is sorted by the value you are looking for.

Linear search sample data: 8 3 9 12 4 10 38 2 1 93 56 34


Binary search sample data: 1 2 3 4 8 9 10 12 34 38 56 93


In the first set of data, unsorted data cannot be searched using binary search. To find the value 38, a program must go through each element until it locates the 7th element; this is a total of 7 iterations. This method is effective when data is constantly being added and removed, and the overhead of a sorting algorithm would be less efficient than a binary search.


In the second set of data, the value 38 can be found by binary search. In the first iteration, a binary halfway point is found (we will choose element 6). Since 9 is less than 38, we know we need to go up. There are six remaining values, so we look at the 9th element (starting from the 6th element, there are six more, so we go half-way, 3 more, a total of 9). Here, we see the value is 34, still less than 38. There are three values remaining, so we go up 2 more. For the third iteration, the value is 56, which is more than our target of 38. Since we advanced 2 last time, we will decrease by 1 this time, and our fourth iteration will find the value 38.


As a matter of fact, in this data set, we will always find our answer in at most 4 iterations, while in the linear search, only the first 4 elements have a chance of being more efficient than the binary search. The problem then comes to down to if the sorting and binary search combined is faster than the linear search. For large data sets that are mostly static, binary searching is preferred. For rapidly changing data sets that would need constant sorting, a linear search may be preferred.


Note that if the data insertion algorithm maintains the sort order (by inserting each element at the correct index in memory), binary searching will likely be faster in the majority of cases. One can use a binary search for data insertion points, keeping the cost of data insertion minimized (but not as efficient as simply appending to the end) while maximizing search capabilities.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why would you write a program using array processing linear search and binary search of an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Best first search program in c?

The best search programs to attempt writing in C are the following: Linear search (simplest), Binary search (faster) Hash search (fastest).


What are the Advantages of binary search on linear search in c?

(i) Binary search can interact poorly with the memory hierarchy (i.e. caching), because of its random-access nature. For in-memory searching, if the interval to be searching is small, a linear search may have superior performance simply because it exhibits better locality of reference. (ii) Binary search algorithm employs recursive approach and this approach requires more stack space. (iii) Programming binary search algorithm is very difficult and error prone (Kruse, 1999).


What is the height of binary search tree in worst case?

In the worst case a binary search tree is linear and has a height equal to the number of nodes. so h=O(h).


It is pre requisite for binary search that the list should be in sorted order but if you have a unsorted list then which searching technique would be better binary search or linear search?

4 more info search how dangerous is the swine flu


Which is faster binary tree or binary search tree?

A tree doesn't do anything so it has no speed...


How many search techniqe's in data structure?

There are two types of searching technique used in data structure.such as linear and binary search.


Write a short note on linear search?

Linear search, also known as sequential search, is a process that checks every element in the list sequentially until the desired element is found. The computational complexity for linear search is O(n), making it generally much less efficient than binary search (O(log n)). But when list items can be arranged in order from greatest to least and the probabilities appear as geometric distribution (f (x)=(1-p) x-1p, x=1,2),then linear search can have the potential to be notably faster than binary search.


Which type of search algorithm checks every element on the list in sequence until a matching data is found?

It's called "Linear Search". If the list is sorted, then it is possible to perform more advanced searches like binary search. If the list isn't sorted, then you can either sort the list first and then binary search or simply use a linear search. Linear search is typically a brute force solution when the data isn't "planned" or if the data is stored in a linked list where random access of the values in the list is slow.


Advantages of binary search over sequencial search?

Linear search takes linear time with a worst case of O(n) for n items, and an average of O(n/2). Binary search takes logarithmic time, with a worst and average case of O(n log n). Binary search is therefore faster on average.


What are the advantages and disadvantages of binary search algorithms?

the major limitation of binary search is that there is a need of sorted array to perform binary search operation. if array is not sorted the output is either not correct or may be after a long number of steps and according to data structure the output should come in minimum number of steps.


What are the advantages and disadvantages of searching in C programming?

If the data is sorted and every element is directly accessible, then you can perform binary search (see built-in function bsearch), otherwise you have to do linear search (which is slower).


How do you run graphics program in C?

pro c language to implement linear search using pointers