What is difference between binary and linear search? And which one is best to use in programming and why and give me some tips | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What is difference between binary and linear search? And which one is best to use in programming and why and give me some tips

26th Jan 2018, 5:54 PM
Muhammad Zubair Irshad
Muhammad Zubair  Irshad - avatar
6 Answers
+ 9
https://techdifferences.com/difference-between-linear-search-and-binary-search.html https://stackoverflow.com/questions/700241/what-is-the-difference-between-linear-search-and-binary-search https://www.geeksforgeeks.org/linear-search-vs-binary-search/ Input data needs to be sorted in Binary Search and not in Linear Search Linear search does the sequential access whereas Binary search access data randomly. Time complexity of linear search -O(n) , Binary search has time complexity O(log n). Linear search performs equality comparisons and Binary search performs ordering comparisons
26th Jan 2018, 5:58 PM
Scooby
Scooby - avatar
+ 5
To clear your concept on this topic, you should follow some "Discrete mathematics" books. *Discrete Mathematics and Its Applications Seventh Edition by Kenneth H Rosen You can try this one.
26th Jan 2018, 6:50 PM
Saleh Ibne Omar
Saleh Ibne Omar - avatar
+ 4
Linear search: You go through each element until you find the one you're searching for. Let's say you have an array and search element x, so you start at element 0 and progress until you find x or reach the end. In binary search you have a tree structure. Imagine having integers and your root is 5. Now this 5 can have two child nodes - the left one is smaller than 5, the right one is bigger than 5. Now you want to go to element 7, so instead of going through all elements of the tree, you start by 5, then go to the right child node. You skip the left node and thereby skip all elements lower than 5 which decreases the search time in the worst case. I'm not good at examples, but I hope it's a bit clearer now.
26th Jan 2018, 6:03 PM
Chris
Chris - avatar
+ 1
Instead of starting from the first element, your starting element can be anywhere. Instead of going through there in a 1 long line, each element has 0, 1 or 2 short paths. While linear search is basically a for-loop, a binary search tree has a more complex algorithm that chooses the correct path through the tree.
26th Jan 2018, 8:32 PM
Chris
Chris - avatar
0
well i m not understand yet
26th Jan 2018, 6:09 PM
Muhammad Zubair Irshad
Muhammad Zubair  Irshad - avatar
0
speed. the main difference is speed of search
26th Jan 2018, 6:23 PM
Василий Савельев
Василий Савельев - avatar